C# Tips that improve code efficiency and productivity

Published Aug 09, 2017

C# is a wonderful programming language; I’ve been writing windows apps and working with C# since C# 3.0 and I like it. The C# syntax in general is very straight forward and simple, in addition, I also like the fact that despite the constantly expanding code library, things are still kept readable. Below I have listed some tips that you can use to improve code efficiency, make your code more compact and achieve productivity. I have no doubt that you know or use one or more already.
1. Ternary Operator (?😃

I first time read about the ternary operator while I was browsing the list of operators in C# back in 2006, it’s an interesting operator and I still use it today in programming tasks. The ternary operator is very straight forward and simple to understand—I’ll give you a few examples to clear up things and make sure you understand this.

1.1 string IsEligibleToPurchaseAlcohol(int age)
I’ve written a function which prints out a string, based on the age value the user enters. I’ve followed the alcohol laws of the United States, this means that there is a law for alcohol minimum purchase age; you have to be 21 or over to be eligible to buy alcohol. Here’s the code written with If-then-else:

string IsEligibleToPurchaseAlcohol(int age)
{
   string message = null;

   if (age >= 21)
   {
        message = "Congratulations! You are eligible to buy alcohol.";
   }
   else
   {
        message = "Sorry! You're not eligible to buy alcohol.";
   }
   
   return message; 
}

And here is the code written in one single return line using the ternary operator, notice that both functions complete the same check.

string IsEligibleToPurchaseAlcohol(int age)
{
    return age >= 21 ? "Congratulations! You are eligible to buy alcohol." : 
                       "Sorry! You're not eligible to buy alcohol.";
} 

1.2 string PMorAM()
In this second example, I am using the ternary operator to determine if it is PM or AM, and return that in a string with current time + “PM” or “AM”.

string PMorAM()
{
    return string.Format("{0}:{1} {2}", DateTime.Now.Hour, 
         DateTime.Now.Minute, DateTime.Now.Hour >= 12 ? "PM" : "AM"); 
}

1.3 bool Is18(int age)
In this third example, I am using the ternary operator to determine if the user is 18 years old or younger, and return true or false.

bool Is18(int age)
{
    return age == 18 ? true : false; 
}

2. Null-Coalesce Operator (??)

Every time we need to test for null values in our code, the null-coalesce operator (??) becomes handy to use. I’ve created 3 code-snippets below, just to demonstrate code efficiency and to show how much shorter the code can become.

2.1 The regular newbie way of testing for null value

object a = null;
object b = new object();
object c;

if (a != null)
    c = a;
else
    c = b; 

It is very obvious that a C# developer that has just learned to use the ternary operator will rewrite this to a single line.

2.2 Using the ternary operator to test null

object a = null;
object b = new object();
object c = (a != null) ? a : b; 

With the null-coalescing operator we can make this shorter. If the left-hand side equals null, the right-hand side will be assigned, in this context object b.

2.3 Using the null-coalescing operator to test null

object a = null;
object b = new object();
object c = a ?? b;
Discover and read more posts from Shyam Shah
get started
Enjoy this post?

Leave a like and comment for Shyam

2
Be the first to share your opinion

Get curated posts in your inbox

Read more posts to become a better developer