NullCheck with C# 6.0

C# 6.0 provides a new way of checking null values compared to its traditional ways.

In earlier versions, to avoid a nullpointerexception, we need to do null checking before invocation of a function.

C# 6.0 provides a null-conditional operator (?) that can be used to avoid writing redundant code every time a null check must be done.

However, developers need to be cautious about its usage and return a value assigned to variables. The null-conditional operator short-circuits the actual operation and performs a null check. The result of the short circuiting is NULL if the operand itself is null. Siince null values cannot be assigned to value types, using a null-conditional operator as shown below would throw a compile time error:

  1. string inputString="Mario";  
  2. int length=inputString?.Length  
In the list of advancements of the null checking, the Null-conditional operator can also be used to check on indexers, in other words check a collection to determine if it is null before retrieving an item from a collection.
  1. T? item = collection?[index];  
The null-conditional index form of the operator ? causes indexing into a collection to occur only if the collection isn't null. That is programmatically equivalent to:
  1. T? item = (collection != null) ? collection[index] : null.  
Note that the null-conditional operator can only retrieve items. It won’t work to assign an item. Also pay attention to the implicit ambiguity when using the ? […] operator on a reference type. Because reference types can be null, a null result from the ? […] operator should not be mistaken with whether the collection was null or the element itself was, in fact, null.

Another common pattern where the null-conditional operator could be used is in combination with the coalesce operator. Instead of checking for null on docList before invoking Length, you can retrieve an item count as follows:
  1. List<string> docList = GetListOfDocuments("c:/temp/");  
  2. return docList?.Count ?? 0;  
In this case, any empty collection (no documents) and a null collection are both normalized to return the same count.

Another highly awaited feature in the list is to check for null before invoking a delegate. Thanks to the Microsoft team because this problem has been persistent since C# 1.0 .

Traditional approach:
  1. public  class Delegate_Example  
  2. {  
  3.    public event EventHandler<float> OnPriceChanged;  
  4.     private int _price;  
  5.     public int Price  
  6.     {  
  7.         get  
  8.         {  
  9.             return _price;  
  10.         }  
  11.         set  
  12.         {  
  13.             EventHandler<float> localOnchanged = OnPriceChanged;  
  14.             if (localOnchanged!=null)  
  15.             {  
  16.                 _price = value;  
  17.                 localOnchanged(this, value);  
  18.             }  
  19.   
  20.         }  
  21.     }  
  22. }  
New approach:
  1. public class Delegate_Example  
  2. {  
  3.       public event EventHandler<float> OnPriceChanged;  
  4.       private int _price;  
  5.   
  6.       public int Price  
  7.       {  
  8.           get  
  9.           {  
  10.               return _price;  
  11.           }  
  12.   
  13.           set  
  14.           {  
  15.               OnPriceChanged?.Invoke(this, value);  
  16.           }  
  17.       }         
  18.   }  
Short summary 
  1. Short-circuit additional invocations in the call chain if the operand is null.

  2. Return null if the operand is null.

  3. Support invocation of dele in a thread safe manner.

  4. Return a nullable type (System.Nullable<T>) if the target member returns a value type.

  5. Available as both a member operator (?.) and an index operator (?[…]).