Recognized by Clutch.co as a top-rated Mobile App Development Company.
folio3-mobile
US 408 365 4638
START YOUR PROJECT
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us
Menu
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us

Top 5 C# Programming Techniques

Published by: Folio3 | May 21, 2021 msaqlain
SCROLL AND BE AMAZED!
Home > Blog > Top 5 C# Programming Techniques

As a mid-level programmer, I always think about being more productive and optimized in my regular day jobs. C# and .NET are deep areas even for accomplished, experienced, developers. Though, I know right, I choose a nice clickbait title, but the tips I am gonna show you are not secretly stashed in the language.

These are some techniques, I have summarized after reading so many countless basic articles & as per my experience. Each tip will be supported by sample code to make it more understandable for the C# newbies to get a hold on.

 

1. Conditional Operators:

The Conditional operator provides a fast and simple way of writing if-else statements:

condition ? first_expression : second_expression;

It means: If the condition is true, return the first expression, else, return the second expression.
In this example, We have an if else statement that demonstrates if the variable number is greater than 5 and depending on the result, displays a particular message.

This code is fairly straightforward, but it could be written in far fewer lines and still be understandable:

 

 

2. Null coalescing operator:

The null coalescing operator returns left-hand operand if the operand isn’t null, and if it is, the right-hand operand is returned. Using ??, we can avoid Null Reference Exception, which is thrown once we attempt to use something which is null.
Practically:

 

3. Null conditional Operator:

The ?, null conditional operator, is a form of a member access operator. With a null conditional operator, the expression class?.students evaluates to students if the left operand, class, is non-null; otherwise, it evaluates to null.

This example shows how to check if book has chapters by using a null conditional operator, so we don’t need to check for nulability of this property.

If the book doesn’t have Chapters, the numberOfChapters will equals 0.

 

4. String Builder:

If you have a situation where you need to append many strings — especially if they are large — use StringBuilder instead of String. String is an immutable type which suggests that whenever you modify its value, you create a replacement String or new String object. Because of that, your application can become very slow once you got to perform this operation repeatedly.

 

5. Avoid race condition with TryGetValue method:

It’s a good idea, to try to write the right safe code wherever possible in your C# application. There are multiple ways of doing that in C# and in .NET.
Take a look at the below image and comments before each line.

The difference is I’ve cached the copy of the value here in this result variable in line#14. So even if the dictionary item gets removed by the time we get to line#16, I’ve got the cached copy here in this result variable.

So, the tip is, use try-get-value, to write the right safe-code and if statements.

These are five straightforward suggestions for making coding easier and more understandable.
Feel free to share your responses and feedback below.
Read more on this topic in the official Microsoft documentation.

Thanks For Reading, Cheers!


About Folio3

Folio3 is a leading full-spectrum mobile app development company that has been delivering innovative experiences in the form of feature-rich, high-performance native and cross-platform applications. With more than 15 years of development experience and more than 1000 successfully delivered projects, our clients only have the best things to say about us, whether they are Fortune 500 companies or growing startups. We like to think of ourselves as your technology partners instead of hired guns and we employ a user-centric approach to designing and developing world-class mobile apps.

Newsletter

Search

Archives

  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • October 2021
  • September 2021
  • May 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • Categories

    • Android App Development
    • App Development
    • App Testing
    • Blog
    • Elasticsearch
    • flutter-app-development
    • IOT
    • React Native
    • Staff Augmentation

Recent Posts

  • Getting started with NgRx
  • Secure Mobile App Credentials in React Native
  • Bugs Count & Test Coverage
  • Introduction to Nessus Vulnerability Scanning Tool
  • Error Boundary in React Native

Tags

  • android
  • Automation
  • cross-platform
  • development
  • firebase
  • ios
  • QA
  • react-native
  • Testing
  • Test Script

Newsletter

Newsletter

Post navigation

Previous Over The Air Updates in React-Native
Next Responsive UI with Flutter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Schedule an appointment with our Mobile App Development Expert

    Footer Menu
    • Company
      • About Us
      • Portfolio
      • Blog
      • Careers
      • Contact Us
    • Solutions
      • Apps Discovery Services
      • Team Augmentation
      • Enterprise App Development
      • AR/VR Application Development
      • IoT Application Development
      • Wearables Apps Development
      • Field Sales
      • On-Demand Apps Development
    • Technologies
      • iOS
      • Android
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • HTML5
      • Sencha
    • Industries
      • Retail
      • Agriculture
      • Healthcare
      • Pharmaceutical
      • Manufacturing
      • Automotive
      • Logistics
      • Education

    US Office

    Belmont, California – 1301 Shoreway Road, Suite 160, Belmont, CA 94002

    Pleasanton, California – 6701 Koll Center Parkway, #250 Pleasanton, CA 94566

    Tel: +1 408 365 4638
    Support: +1 (408) 512 1812

    Mexico Office

    Amado Nervo #2200, Edificio Esfera 1 piso 4, Col. Jardines del Sol, CP. 45050, Zapopan, Jalisco, Mexico

    Bulgaria Office

    49 Bacho Kiro Street, Sofia, 1000, Bulgaria

    Canada Office​

    895 Don Mills Road, Two Morneau Shepell Centre, Suite 900, Toronto, Ontario, M3C 1W3, Canada

    UK Office

    Export House, Cawsey Way, Woking Surrey, GU21 6QX

    Tel: +44 (0) 14 8361 6611

    UAE Office

    Dubai, UAE – Dubai Internet City, 1st Floor, Building Number 12, Premises ED 29, Dubai, UAE

    Tel: +971-55-6540154
    Tel: +971-04-2505173

    Pakistan Office

    163 Bangalore Town, Main Shahrah-e-Faisal, Karachi –
    75350

    705, Business Center, PECHS Block-6, Shahrah-e-Faisal,
    Karachi – 75350

    First Floor, Blue Mall 8-R, MM Alam Road Gulberg III, Lahore

    Tel: +92-21-3432 3721-4 

    © 2021, Folio3 Software Inc., All rights reserved.

    • Privacy policy and terms of use
    • Cookie Policy
    Follow us on
    Facebook-f
    Twitter
    Linkedin-in

    Get a free app audit

      Tired of your app not performing up to the mark?

      Get a free technology and app strategy review.