Site icon Mobile App Development Services

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 StringString 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!

Exit mobile version