Null Conditional Check in C# 6

Continuing with the features of C# 6.0, we will now explain the concept of the Null conditional operator.

Before we explain this feature, let's see why we need this feature. Assume we have a class with two properties.

  1. public class UserData  
  2. {  
  3.     public String Username { getset; }  
  4.     public String Password { getset; }  
  5.     public String Token { getset; }  
  6. }  

We create an instance of this class but do not assign any value to the Token property. So it will be NULL. So in our code, when we use these properties, we add the NULL check for the Token property. 

  1. namespace ConsoleApp  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             UserData _userData = new UserData();  
  8.             _userData.Username = "User1";  
  9.             _userData.Password = "TestPassword";  
  10.   
  11.             if (_userData.Token == null)  
  12.             {  
  13.                 Console.Write("Token not provided");  
  14.             }  
  15.             else  
  16.             {  
  17.                 Console.Write("Token Initialized");  
  18.             }  
  19.             Console.ReadKey();  
  20.         }  
  21.     }  
  22.     public class UserData  
  23.     {  
  24.         public String Username { getset; }  
  25.         public String Password { getset; }  
  26.         public String Token { getset; }  
  27.     }  
  28. }  

Look's good and works fine. With C# 6.0, this NULL check process is made more simplified. In order to check any value, we can simply use the convention:

instanceName.?PropertyName

Confused? Let's replace the preceding convention with the code instanceName => userData and PropertyName => Token. So we change our code to C# 6.0 and check the NULL using the preceding convention.  So our code becomes:

  1. using static System.Console;  
  2.   
  3. namespace NullOperator  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             UserData _userData = new UserData();  
  10.             _userData.Username = "User1";  
  11.             _userData.Password = "TestPassword";  
  12.   
  13.             Console.Write(_userData?.Token);   
  14.             Console.ReadKey();  
  15.         }  
  16.     }  
  17.     public class UserData  
  18.     {  
  19.         public String Username { getset; }  
  20.         public String Password { getset; }  
  21.         public String Token { getset; }  
  22.     }  
  23. }  
So what we did is, we simply add "?" to check the value of the Token and write it to the Console, if it is available else do nothing. Let's add the binary operator to print the message if no token is available. So we simply add the message as:
  1. Console.Write(_userData?.Token ?? "Token not provided");  
Run the code and see the results. 
 
 

Figure 1: Result
 
Easy to use, isn't it?

Up Next
    Ebook Download
    View all
    Learn
    View all