Automatic null checking in C# 6.0

 May 30, 2016

As programmers we are used to checking a variable value for null before we use it in a subsequent block of code. It's one of the most repetitive tasks we do every day. In today's blog I will introduce you to the new question mark dot operator introduced in C# 6.0. You will be able to complete this repetitive null checking task in an easier way.

Let's say you have a class Product with a property Title that could be null if was not yet initialised. Now let's say you have a block of code inside a method of the class Product that only makes sense if the Title of the product is not an empty string. One way to check it is to check if the Length of the Title is bigger than zero. The problem is that I can only get the Length if the Title is not itself a null. So the usual way to write the conditional is as follows:

if    (    this.Title != null
& &      this.Title.Length > 0 )
{
            //here I can safely use the Title...
}

If I just check whether or not this.Title.Length > 0 I will get an exception whenever the property Title was not initialised. So we do need to include the this.Title != null part before looking into the Length. This pattern of checking if something is not null before retrieving the value of a property of it is prevalent and leads to the tedious null checking condition appearing everywhere.

The question mark dot operator addresses this issue in an elegant way as you can see in the following code snippet.

if    (    this.Title?.Length > 0 )
{ ... }

On the left of ?. we have something that could be null or not, but when it is not null we have on the right of the operator a property we want to evaluate. If the left hand side was a null the Length is never evaluated and the conditional automatically evaluates to false. So no exceptions are raised if the Title is not initialised, we simply do not execute the block of code in the body of the if statement.

Another very useful application of the ?. operator is to check if there is a listener to an event before triggering the event. The following code snippet illustrates this usage.

OnSaveProduct?.Invoke(this, args);

The method ,em>Invoke is only called if the delegate OnSaveProduct is not null.

The ?. operator is a recent addition to C#, but it is worth to remind you of the null-coalescing operator ?? that has been available since Visual Studio 2010. The next code snippet shows the ?? operator in action.

Console.WriteLine( product.Title ?? "NA" );

If the Title is not null we display its value, otherwise we display NA.

To learn more about C# we recommend the course 20483: Programming in C#.

How do your Excel skills stack up?   

Test Now  

About the Author:

Newton Godoy  

With over 17 years of in-class training experience and over 16 years of industry experience, Newton offers students a wealth of real-world technical knowledge and expertise in the areas of .NET application development, SQL Server and SharePoint Server. After spending several years lecturing as a professor, Newton found his true calling and began his career as a MCT. He worked as a technical trainer for some of Brazil's and Australia’s largest corporate training organisations before finally finding a home with New Horizons where he is now one of our top trainers. Newton brings a thorough mentoring capability to the classroom where he can advise on technical issues and challenges often beyond the scope of the course curriculum. His combination of technical knowledge and instructor experience make him one of the most respected instructors within the IT training industry.

Read full bio
top
Back to top