The null coalescing operator represented by the symbol ?? is a C# operator, which provides a handy way to set the default value of a variable in an expression. It takes two operands, if the left operand is null, then the right operand’s value is returned else the left operand’s value.
It’s a precise way to say, “If this value is null, then use the other value.“
Syntax of null coalescing operator (??) in C#:
The null coalescing operator has following syntax.
First Expression ?? Second Expression ( If first expression is null, then evaluate the second expression. Otherwise evaluate first expression and ignore the second expression)
x = y ?? z
This implies, if y is null then assign z to x else assign y.
int? y = null;
int z = y ?? 500;
Console.WriteLine("Value of z is : {0}", z);
This generates below output.

In above,
- The variable y is a nullable integer type with null value as assigned.
- In the next line int z = y ?? 500 , y is null so second operand value is returned, and hence z is assigned with 500.
null_coalescing_operator_1.1 - It prints “Value of z is : 500” while running.
Usage of Null Coalescing Operator (??):
The null coalescing operator (??) provides a cleaner way to handle null values and assign default values when necessary. let’s look at an example of null check without using null coalescing operator.
string InPutFileName = null;
string FileName;
if (InPutFileName != null)
FileName = InPutFileName;
else
FileName = "InputFile.txt";
Console.WriteLine("File Name : {0} ", FileName);
If you run above example, then you will get below output.

We can use null coalescing operator (??) in above case to remove the extra null check and also specify the default assignment in a single statement, as follows.
//Use of null-coalescing operator (??) string InPutFileName = null; string FileName = inPutFile ?? "InputFile.txt";
This reduces the extra null check and make the code look simpler.
Null coalescing operator (??) in composite scenario:
You can also use this operator in composite scenarios as follows.
string k = x ?? y ?? z;
Above chaining assignment expression evaluates to x, if x is not null else evaluates to y, if y is not null; otherwise, it evaluates to z.
Lets check an example.
using System;
namespace NullCoalescingOperator
{
class Program
{
static void Main(string[] args)
{
string FileName = string.Empty;
//Null coalescing operator in chaining sceanrio
FileName = GetFileNameFromDb() ?? GetRandomFileName() ?? "DefaultTextFile.txt";
Console.WriteLine("?? Opearator in Composite Scenarios:");
Console.WriteLine("******************************************");
Console.WriteLine("GetRandomFileName() : {0} ", GetRandomFileName());
Console.WriteLine("File Name : {0} ", FileName);
Console.Read();
}
public static string GetFileNameFromDb()
{
return null;
}
public static string GetRandomFileName()
{
return "FileName.txt";
}
}
}
When we run above example, it produces below output:

In here, the composite scenarios expression evaluates to “FileName.txt” . The first expression, the function GetFileNameFromDb() returns null value and the next expression, the function GetRandomFileName() returns value “FileName.txt”.
One thing to note here, in scenarios if method GetRandomFileName() also returns null value then the expression result will be “DefaultTextFile.txt”.
Null coalescing operator (??) with Nullable Type:
The null coalescing operator can be used with reference type as well as nullable value types, let’s take an example how it works with the nullable types.
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, int? i declared as a nullable type
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 valid value, then assign the value to variable j and if i doesn’t have any value or null then assign 100 to j .
This conditional check can be re-written using null coalescing operator (??) as follows.
int j = i ?? 100;
In the above, compiler checks nullability of variable i and then assigns 100 to variable j.

Null Coalescing Assignment Operator (??=)
C# 8.0 introduced a new operator called as null coalescing assignment operator ( ??=), which provides a more concise way to handle nulls. It has following syntax.
x ??= Set some default value // This implies, if x is null then set some default value.
Let’s take below example.
//Use of null-coalescing assignment operator (??) string fileName = null; fileName ??= "InputFile.txt";
In the above case, FileName is null, hence“InputFile.txt” assigend to it.
Conclusion:
The null coalescing Operator in C# is a handy tool that simplifies the handling of null values in your code. It provides a concise way to assign default values that makes your code more robust, readable and maintainable.
Thanks for your time 🙂 feel free to provide your feedback and do check my other blogs on C# operator.
Null-Conditional Operator in C# (?.)
Conditional Operator in C# (?.)
