New Features in C# 6.0 – Null-Conditional Operator


This is blog post series about new features coming in the next version of C# 6.0. The first post is about null conditional operator.

The NullReferenceException is night mare for any developer specially for developer with not much experience. Almost every created object must be check against null value before we call some of its member. For example assume we have the following code sample:

class Record
{
 public Person Person  {get;set;}
 public Activity Activity  {get;set;}
}
public static PrintReport(Record rec)
{
  string str="";
   if(rec!=null && rec.Person!=null && rec.Activity!=null)
   {
     str= string.Format("Record for {0} {1} took {2} sec.", rec.Person.FirstName??"",rec.Person.SurName??"", rec.Activity.Duration);
     Console.WriteLine(str);
   }

  return ;
}

We have to be sure that all of the object are nonnull, otherwize we get NullReferenceException.

The next version of C# provides Null-Conditional operation which reduce the code significantly.

So, in the next version of C# we can write Print method like the following without fear of NullReferenceException.


public static PrintReport(Record rec)
{
  var str= string.Format("Record for {0} {1} took {2} sec.", rec?.Person?.FirstName??"",rec?.Person?.SurName??"", rec?.Activity?.Duration);
     Console.WriteLine(str);

  return;
}

As we can see that ‘?’ is very handy way to reduce our number of if statements in the code. The Null-Conditional operation is more interesting when is used in combination of ?? null operator. For example:


 string name=records?[0].Person?.Name??"n/a";

The code listing above checks if the array of records not empty or null, then checks if the Person object is not null. At the end null operator (??) in case of null value of the Name property member of the Person object put default string “n/a”.

For this operation regularly we need to check several expressions agains null value.
Happy programming.

 

About Bahrudin Hrnjica

PhD in Mechanical Engineering, Microsoft MVP for .NET. Likes .NET, Math, Mechanical Engineering, Evolutionary Algorithms, Blogging.

Posted on 01/10/2014, in .NET, C#, CodeProject and tagged , , , . Bookmark the permalink. 6 Comments.

  1. Feels like a more concise syntax at the expense of readability. Think I prefer the Null Object pattern over this.

  2. Last paragraph : “The code listing above checks if the array of records not empty or null,” – this is incorrect. it is not checking if the array is empty.

  1. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1708

  2. Pingback: Les liens de la semaine – Édition #100 | French Coding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s