Click here to Skip to main content
15,121,230 members
Articles / Programming Languages / C# 6.0
Tip/Trick
Posted 22 Oct 2015

Stats

13.9K views
3 bookmarked

Better way of handling Null Reference Checks in C#6.0

Rate me:
Please Sign up or sign in to vote.
4.57/5 (3 votes)
22 Oct 2015CPOL3 min read
The tip is about using Elvis operator which is introduced in C# 6.0 for Null Reference Checks. It gives an example of how to use it.

Introduction

C# 6.0 has introduced a operator which can make coding a bit simpler and with less code. This operator is called as Elvis Operator (not officially declared) but due to its looks.  =>  ?.  two eyes and elvis hairstyle

The Elvis Operator can be used to remediate the most dreaded error in C# System.NullReferenceException or simply Object reference not set to an instance of an object. The issue occurs most of the times if null reference checks are not properly handled before using an unassigned object reference. Null reference checks are time consuming code and also leads to lot of programming. They can be done before passing parameters to a method, concatenating strings for display /messages or assigning it to local variables.

Problem Statement

Lets take a simple example where we need to display visitor details. I have created a console application for this example but this error can happen in any application type where C# code is used.

Here Visitor is a class having 2 properties 

  1. Name 
  2. Contact Details 

Contact Details is also a class having 2 properties

  1. MobileNo
  2. EmailAddress
C#
class Visitor
{
    public string Name { get; set; }   
    public ContactDetails ContactDetails { get; set; }
}

class ContactDetails
{
    public string Mobile { get; set; }
    public string EmailAddress { get; set; }
}

Now let's enter a Visitor Named Yash who has just provided his Mobile Number

The below code will throw System.NullReferenceException since ContactDetails is null and we are trying to access its properties using Visitor Object Reference (visitorObj)

C#
class Program
    {
        static void Main(string[] args)
        {
            Visitor visitorObj = new Visitor();
            visitorObj.Name = "Yash";
            visitorObj.ContactDetails = null;

           Console.WriteLine(String.Format("Name {0} Mobile No {1} Email Address {2}", visitorObj.Name, visitorObj.ContactDetails.Mobile, visitorObj.ContactDetails.EmailAddress));
            Console.ReadLine();
       
        }
    }

Solution 1 (without Elvis)

To avoid this error, we need to put some null reference checks.

But before that let's analyze how many possibilities there can be in a program which can lead to null reference error

All these conditions may not be required for this program example but it is a good practice to handle them since one may move the code to different assembly/class and that time it would become essential.

  1. Visitor Object Reference (visitorObj) can be null if no visitor object is created
  2. visitorObj.Name
  3. visitorObj.ContactDetails
  4. visitorObj.ContactDetails.Mobile
  5. visitorObj.ContactDetails.EmailAddress

So to write a program which can cater all the possibilities mentioned above we need to write multiple if-else loops or use ternary operators. Let's take hybrid approach to reduce code.

C#
   class Program
    {
        static void Main(string[] args)
        {
            Visitor visitorObj = new Visitor();
            visitorObj.Name = "Yash";
            visitorObj.ContactDetails = null;

            if(visitorObj != null)
            Console.WriteLine(String.Format("Name {0} Mobile No {1} Email Address {2}", 
visitorObj.Name!=null?visitorObj.Name:null, 
visitorObj.ContactDetails != null ? visitorObj.ContactDetails.Mobile !=null? visitorObj.ContactDetails.Mobile :null: null,
                visitorObj.ContactDetails != null ? visitorObj.ContactDetails.EmailAddress != null ? visitorObj.ContactDetails.EmailAddress : null : null));
            else
            Console.WriteLine("The object is not created");

            Console.ReadLine();
           
        }
    }

But you can see how complicated the above code has become due to nested ternary conditions.

Solution 2 (with Elvis)

So lets see how Elvis operator is going to help us in this

?. operator will first compare the left part of the operator with null if not then will get the right part of the operator

For e.g. val = x?.y means

C#
if(x!=null){
    val=x.y; }
else val = null;

Or

C#
val = (x!=null)?x.y:null ;

So when we substitute Elvis Operator we get the following:-

C#
Visitor visitorObj = new Visitor();
visitorObj.Name = "Yash";
visitorObj.ContactDetails = null;

Console.WriteLine(String.Format("Name {0} Mobile No {1} Email Address {2}", visitorObj?.Name, visitorObj?.ContactDetails?.Mobile, visitorObj?.ContactDetails?.EmailAddress));
Console.ReadLine();

Here string x = visitorObj?.ContactDetails?.Mobile means 

C#
if(visitorObj!=null && ContactDetails !=null) 
string x = visitorObj.ContactDetails.Mobile;
   
 or

string x =(visitorObj != null)?(ContactDetails !=null)?ContactDetails.Mobile:null:null;

The above code is much clearner and easy to implement plus it is taking care of all the null reference checks in the application. The Elvis Operator can be very useful and handy to avoid most of the Null Reference Check Errors. The limitation of the Elvis operator can be seen is that it will assign "null" as default value when the left side of the operator is null. Also it does not give any flexibility like if-else wherein you can write code in else part also.

Coding is Simple

Image 1

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

yash soman
Technical Lead
India India
I have worked as a technical lead for MNC's. Currently, I conduct trainings on Microsoft Technologies at Ambrosia Knowledge Center. I am interested in learning new technologies,frameworks, designs etc. I like to distribute whatever knowledge i have so that more people can contribute and innovate things for our future. If you are facing any issues or problems and i could be help do feel free to connect me on my email link Send Mail.

My blog is Coding is Simple

Comments and Discussions

 
GeneralWhy not create a ctor? Pin
Heiko Scholze5-Nov-15 6:45
MemberHeiko Scholze5-Nov-15 6:45 
GeneralRe: Why not create a ctor? Pin
yash soman6-Nov-15 19:48
professionalyash soman6-Nov-15 19:48 
GeneralI don't understand the fuss Pin
Afzaal Ahmad Zeeshan23-Oct-15 0:25
mveAfzaal Ahmad Zeeshan23-Oct-15 0:25 
GeneralRe: I don't understand the fuss Pin
yash soman23-Oct-15 1:22
professionalyash soman23-Oct-15 1:22 
GeneralRe: I don't understand the fuss Pin
John Brett23-Oct-15 1:58
MemberJohn Brett23-Oct-15 1:58 
GeneralRe: I don't understand the fuss Pin
yash soman23-Oct-15 18:07
professionalyash soman23-Oct-15 18:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.