Why you should use C# Null Coalescing and Null Conditional Operators?

  1. Null Coalescing operator:

Null coalescing operator is new features of C# 6.0. It uses double question mark (??). It is shorthand of If condition and ternary operator. It is used for null check. There are different ways to check null in C# application.

  • Null check using if else: This is traditional method to check null.

string name = Console.ReadLine();

//Null check using if else condition.

if (!string.IsNullOrEmpty(name))

{

Console.WriteLine(“If Condition:” + name);

}

else

{

Console.WriteLine(“Else condition: Default value”);

}

Input: name=”Ram”;

Output: “If Condition:” Ram

Input: name=null;

Output: “Else condition: Default value”

  • Null check using ternary operator

//Null check using ternary operator

string value2 = name != null ? name : “Default”;

Console.WriteLine(“Ternary operator:” + value2);

Input: name= Ram;

Output: Ternary operator: Ram

Input: name=null;

Output: Ternary operator: Default

  • Null check using Null Coalescing

//Null check uisng Null coalescing operator

string value1 = name ?? “Default”;

Console.WriteLine(“Null Coalescing:” + value1);

Input: name= Ram;

Output: Null Coalescing: Ram

Input: name=null;

Output: Null Coalescing: Default

//Employee class

public class Employee

{

public string EmpName {get; set;}

public decimal Salary {get; set;}

y}

//Nested Null coalescing operator

string val1 = null;

string val2 = null;

string val3 = null;

string val4 = null;

var nestedNullVal = val1??val2??val3??val4??”Undefine”;

Output: Undefine

Console.WriteLine(“Nested Value: ” + nestedNullVal);

Employee employee = new Employee();

2. Null Conditional Operator ?. and ?[]

It is available in C# 6.0 and later, it is applicable to member access (?.) and element access (?[]). It checks for null value.

I. Member access: ?.

It is used for null check of entity when we access property inside it.

Traditional if else null check

//Null conditional operator

var val = employee?.EmpName;

//Nested null conditional operator

var nullCond = employee?.EmpName?.Length;

Here we are accessing EmpName property of Employee Class. We are also finding length of EmpName property in next line.

II. Element access: ?[]

Null check of Array or list element can be done using element access.

List<Employee> employeeList = new List<Employee> {

new Employee

{

EmpName=”rajesh”,Salary=10000

}

};

var emp = employeeList?[0].EmpName;

Console.WriteLine(emp);

Here employeeList is not null then access EmpName of first element of list.

Output: emp = Rajesh

employeeList = Null;

If employeeList is null then

Output: emp=null

Conclusion:

  • Null Coalescing is used to check null of object.
  • Null Coalescing can be nested.
  • Null Conditional Operators is used to check null of object when we access member or element of it.  
  • Null Conditional Operators also can be nested.
  • Both reduces line of codes.

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*