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 Expression ( If 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.
1 2 3 |
int? y = null; int x = y ?? 100; Console.WriteLine("The Value of 'x' is:" + x); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; namespace NullCoalescingOperator { class Program { static void Main(string[] args) { string inPutFile = string.Empty; string FileName = string.Empty; //Fetch Filename inPutFile = GetInputFileName(args); //Check if filename is null and then assign the default value if (inPutFile == null) FileName = "InputFile.txt"; //Set the default value else FileName = inPutFile; //Set the fileName as returned Console.WriteLine("File Name : {0}", FileName); Console.Read(); } //function returns null if the file name is not in the string array public static string GetInputFileName(string[] args) { if (args.Length > 0) return Convert.ToString(args[0]); return null; } } } |
If you run above example, then you will get below output.
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.
1 2 3 |
//Check if filename is null and then assign the default value //Use of null-coalescing operator (??) FileName = inPutFile ?? "InputFile.txt"; |
The complete sample code is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; namespace NullCoalescingOperator { class Program { static void Main(string[] args) { string inPutFile = string.Empty; string FileName = string.Empty; //Fetch Filename inPutFile = GetInputFileName(args); //Check if filename is null and then assign the default value //Use of null-coalescing operator (??) FileName = inPutFile ?? "InputFile.txt"; //Set the default value Console.WriteLine("Coaleascing Operator (??) Demo"); //Displayes as "File Name : InputFile.txt" because inPutFile value as null. Console.WriteLine("Input File Name : {0}", FileName); Console.Read(); } //function returns null if the file name is not in the string array public static string GetInputFileName(string[] args) { if (args.Length > 0) return Convert.ToString(args[0]); return null; } } } |
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” .
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
namespace NullCoalescingOperator { class Program { static void Main(string[] args) { //Declared a nullable type int? i = null; //checking if the nullable type has valid value //and setting the default value int j = (i.HasValue) ? i.Value : 100; //Coalescing operator, which can replace above //(HasValue) checking and resetting the value. int k = i ?? 100; } } } |
In the above code snippet, we have declared a nullable type as int? i and a variable of type int as int j
1 |
int j = (i.HasValue) ? i.Value : 100; |
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.
1 |
int k = i ?? 100; |
In the above, compiler checks nullability of variable
i and then assigns 100 to variable k
.

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# (?.)