Null-Coalescing Operators in C#
Vaibhav • September 11, 2025
In programming, dealing with missing or unknown values is a common challenge. Sometimes a variable might not have a
value yet, or it might be intentionally left empty. In C#, this situation is represented using the special keyword
null
. But working with null
can be tricky - if you
try to use a null value without checking it, your program might crash. That’s where null-coalescing operators come
in.
In this article, we’ll explore two powerful tools for handling null values safely and easily: ??
and ??=
. These operators help you write cleaner,
safer code by providing fallback values and assigning defaults when needed. We’ll stay within the concepts you’ve
already learned - variables, data types, expressions - and avoid anything we haven’t covered yet, like methods or
control structures.
What Is null
?
In C#, null
means “no value.” It’s like an empty box - the box exists, but there’s
nothing inside. You can assign null
to variables that are allowed to be empty, such
as strings or nullable types.
string name = null;
int? age = null;
In this example, name
is a string with no value, and age
is a nullable integer that also has no value. These variables exist, but they don’t
hold any actual data yet.
The ??
Operator - Providing a Fallback
The ??
operator is used to provide a fallback value when something is null
. It’s like saying, “If this value is missing, use this other one instead.”
string userName = null;
string displayName = userName ?? "Guest";
Here, userName
is null
, so displayName
becomes "Guest"
. If userName
had a value, that value would be used instead.
The ??
operator checks the value on the left. If it’s not null
, it returns that value. If it is null
, it returns
the value on the right.
The ??
operator works with any type that can be null
, including strings, nullable numbers, and other reference types.
Using ??
with Nullable Numbers
You can use ??
with numeric types that allow null
,
such as int?
, double?
, or decimal?
.
int? score = null;
int finalScore = score ?? 0;
In this case, score
is null
, so finalScore
becomes 0
. If score
had a value like 85
, that value would be used
instead.
Chaining ??
for Multiple Options
You can chain multiple ??
operators to check several values in order. This is
useful when you have more than one possible source of data.
string first = null;
string second = null;
string third = "Fallback";
string result = first ?? second ?? third;
This expression checks first
, then second
, and
finally third
. Since the first two are null
, result
becomes "Fallback"
.
Use ??
to make your code more readable and avoid
manual checks for null
. It’s a clean way to provide default values.
The ??=
Operator - Assigning a Default
The ??=
operator is used to assign a value to a variable only if it’s currently
null
. It’s like saying, “If this variable is empty, fill it with this.”
string title = null;
title ??= "Untitled";
After this line, title
becomes "Untitled"
. If
title
already had a value, it would stay unchanged.
This operator is useful when you want to make sure a variable has a value, but you don’t want to overwrite it if it already does.
string caption = "Welcome";
caption ??= "Default"; // caption stays "Welcome"
In this case, caption
already has a value, so ??=
does nothing.
The ??=
operator changes the variable only if it’s null
. It’s a shortcut for assigning a default without extra steps.
Using ??=
with Nullable Numbers
Just like ??
, the ??=
operator works with nullable
numeric types. You can use it to ensure a number has a value before using it.
int? quantity = null;
quantity ??= 1;
Now quantity
becomes 1
. If it had a value like
5
, it would stay unchanged.
Combining ??
and ??=
You can use both operators together to check and assign values in a flexible way. First, use ??=
to make sure a variable has a value. Then use ??
to provide a fallback when using it.
string label = null;
label ??= "Default";
string output = label ?? "Unknown";
In this example, label
becomes "Default"
, and
output
uses that value. If label
had stayed null
, output
would become "Unknown"
.
The ??
and ??=
operators are often used when working with user input, configuration settings, or optional data - anywhere a value
might be missing.
Real-World Analogy
Imagine you’re packing for a trip. You check your bag for a flashlight. If it’s missing, you pack a spare one.
That’s like using ??
. If your bag already has a flashlight, you leave it alone.
That’s like using ??=
. Both help you avoid surprises when you need something later.
Common Mistakes to Avoid
One common mistake is using ??
with types that can’t be null
, like regular int
or bool
. These types always have a value, so ??
won’t do
anything useful. Use nullable types like int?
or bool?
instead.
Another mistake is forgetting that ??=
changes the variable. If you don’t want to
modify the original value, use ??
instead.
Summary
The null-coalescing operators ??
and ??=
make it
easy to handle missing values in C#. Use ??
to provide a fallback when a value is
null
, and use ??=
to assign a default only if
needed. These tools help you write safer, cleaner code without extra checks or complexity. As you build more
advanced programs, these operators will become essential for working with flexible and optional data.