Null conditional operator (?.) is another useful addition made to C# 6.0, it allows developers to write cleaner and concise code. We will explore more in detail.

In some situations, whenever you invoke a method or property on a object that is null. In that case, run-time throws a Null Reference exception. In-that situation you have to write explicit null-ability check of the object before invoking method or property.

To address the run-time null reference exception issue and to reduce the duplicate code for each null check , C#6.0 introduced null-conditional operator (?.) .

This operator (?.) verifies null ability of the operand before calling the method or property.

Syntax and usage

It uses a question mark ? and member access operator . (the dot) as follows.

expression1 ?. expression2  (This expression evaluates to expression2, if expression1 is Not-Null. else, it evaluates to null.)

Example -1

Lets look at below example, In here, we have declared an Employee class. We are accessing employee property by doing a null check using (Conditional operator (?:)) as follows.

In the above example, this line string empName = (emp != null) ? emp.Name : null; evaluates as, If emp is not null then assign emp.Name value to empName else assign null .

If you run above example then null value will be assigned to empName variable. Lets, re-write above statements using null-conditional operator (?.) as follows.

In the above example, we are using Null-Conditional operator (?.) to replace that extra null check condition. This code statement empName = emp?.Name; evaluates as, If emp object is not-null then invoke the property and assign emp.Name value to empName variable else assign null.

The usage of Null-Conditional operator (?.) here allows cleaner  and concise code and also removed extra null conditional check. 

Example -2 – Null-Conditional operator (?.)

In below example, we have created an Employee instance as emp and set emp.Name property value to “John” and emp.Age property value to 45 . We have set emp.PresentAddress property value to null .

When we run above sample code it will generate below out put.

Null-Conditional_Operator_1.3

You can see in the out put window, we are able to fetch Name and Age property value correctly using, However Address  properties are displayed as empty. Lets see why this happened here ? 

Here, we created a new Employee instance Employee emp = new Employee(); and assigned values to Name and Age properties. One thing you can note here is that, the emp object is not null, that’s why emp?.Name and emp?.Age evaluated to valid values.

In case of emp.PresentAddress , even though emp object is not null , but since we have set emp.PresentAddress = null , therefor the address properties emp?.PresentAddress?.City , emp?.PresentAddress?.State , emp?.PresentAddress?.Country and emp?.PresentAddress?.Zip weren’t evaluated and set to default empty value . 

Key Points to be noted :

Below points should be noted while using this operator (?.)

  • When you access an object property that returns value type, in that case null conditional operator (?.) will return nullable  version of that type.

For example, In below case, Age property is a value type and emp?.Age returns int? (nullable type of int ) and not an int . So while assigning it to a variable then you have to declare the variable as nullable type. In below case we have declared int? employeeAge as Nullable type.

Null-conditional_Operator_1.0

It throws a compile error if you don’t specify the nullable type.

Null-conditional operator
  • You can chain multiple null conditional operator (?.) in an expression , If the first operand is null, then the expression evaluation is short-circuited, and no further invocation within the call chain occurs. 

For example,  when we invoke emp?.PresentAddress?.State; in below example code, then both PresentAddress and State property will be invoked only if emp is not null. If emp is null then no further call in the expression will occur.

Use of Null-Conditional Operator(?.) with Member Access operator (.)

If you club null conditional operator (?.) and member access operator (.) together, then during assignment expression the resulting expression will return null , if the left operand evaluates to null. However it will throw a Null Reference Exception if the left operand of (?.) evaluates to null .

For example: emp?.PresentAddress.State returns null when emp object is null . However emp.PresentAddress?.State throws a Null Reference Exception when emp object is null.

Thanks for your time 🙂 feel free to provide your feedback and do check my other blogs on Null Coalescing Operator (??) in C#  and  Conditional Operator in C# (?.)