Null coalescing operator (??) is a C# operator, which is represented by the symbol ?? . It is generally used to set the default value of a variable. It takes two operands, if the left operand is null, then the right operand is returned else the left operand. 

Its a precise way to say that “If this value is NULL, then use the other value”

This is an extension to the article on nullable types, If you haven’t gone through the article then please have a look here .

Syntax and usage of (??)

The null-coalescing operator provides a very convenient way to set a variables default value, it has following form. 

First Expression ?? Second ExpressionIf first expression is NULL , then evaluate the second expression. Otherwise evaluate first expression and ignore second expression)

For example:  x = y ?? z  // This implies, if y is null then assign z to x else assign y.

In the above example, we have an integer variable y  that is a nullable type and has a null value as assigned. So in the assignment statement , the variable y  is assigned with the value “100“.

When we run above code, then it prints “The Value of ‘x’ is: 100” .

Null coalescing operator (??) will be handy while doing null check of a variable during assignment operation. lets look at an example of null check in usual way.

Example -1 : Usual ways of Null check: 

Lets check an example of null check in usual way, in below sample code.

  • We have a function as GetInputFileName(string[] args) , it returns string value if input array has data or else returns null value.
  • In the main method, we retrieve the data in variable string inPutFile by calling the method.
  • Now, if inPutFile is null then assign “InputFile.txt” or else assign inPutFile value to the fileName variable.

If you run above example, then you will get below output. 

Null Coalescing Operator_1_2

In the above console, we get the default file name as “InputFile.txt” . Here, we performed usual ways of null checking and then assigned the default value. 

Example -2 : Null check using null coalescing operator (??) :

We can use null coalescing operator (??) in above case to remove the extra null check and also can specify the default assignment in a single statement, as follows.

The complete sample code is as follows.

When you run above sample code then you will get below output, here if you see that the value of inPutFile variable is null and hence the default value of the variable FileName is being set to “InputFile.txt” .

Null Coalescing ?? Operator_1_3

The null-coalescing operator can be used with a  reference type as well as nullable value types. Lets look at below example, how it works with nullable type.

Example -3 : Null coalescing operator (??) with Nullable Type:

Lets look at below code where we have used nullable value types.

In the above code snippet, we have declared a nullable type as int? i  and a variable of type int as int j 

In above line of code, we are using conditional operator (?:) to check if the nullable type has valid value or not. If the variable i has some valid value, then assign it to variable j and if i is null then assign 100 to j .

Since i is a nullable value type and we have set the default to null , so when we run above code then it assigns 100 to variable j .

This conditional check can be re-written using null-coalescing operator (??)  as follows.

In the above, compiler checks nullability of variable i and then assigns 100 to variable k .

Null Coalescing Operator example 1.0
Null Coalescing Operator example 1.0

Key points :

  • Null-Coalescing Operator can be used in both reference types and nullable value types.
  • It works better with expressions and much easier to read and better to understand.

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