Skip to main content

How to Use the Null-Coalescing Operators ?? and ??= in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A missing value often has an obvious replacement: no nickname, so say "friend"; no title, so use "Untitled". In C#, ?? gives a value a fallback for when it's null, and ??= assigns a variable only when it's still null.

Syntax​

Syntax
<result> = <value> ?? <fallback>;
<variable> ??= <fallback>;
PartWhat it does
<value> ?? <fallback>Returns <value> when it isn't null, and <fallback> when it is.
<variable> ??= <fallback>Assigns <fallback> to <variable>, but only when <variable> is null.
<fallback>Evaluated only when it's needed.

Microsoft's null-coalescing operators reference: "The null-coalescing operator ?? returns the value of its left-hand operand if it's not null. Otherwise, it evaluates the right-hand operand and returns its result." And: "The null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null."

The Example​

The example greets someone by their nickname, or as "friend" when there's none, before and after a nickname is set. Then it gives an empty title the default Untitled with ??=.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints nickname ?? friend while nickname is null and again after it's set to Lee, then sets a null title to Untitled with ??= and prints it.

main.cs
string? nickname = null;
string display = nickname ?? "friend";
Console.WriteLine(display);

nickname = "Lee";
Console.WriteLine(nickname ?? "friend");

string? title = null;
title ??= "Untitled";
Console.WriteLine(title);

Step 2: Run It​

Terminal
dotnet run main.cs
Output
friend
Lee
Untitled

A zsh terminal where dotnet run main.cs prints friend, Lee and Untitled.

While the nickname was null, ?? returned the fallback, friend. Once it was Lee, the same expression returned Lee. title ??= "Untitled" filled in the null title. Had title already held a value, ??= would have left it alone. The command is the same in zsh, bash and PowerShell.

An Empty String Isn't null​

?? only replaces null. An empty string is a real value, so it stays:

main.cs
string? nickname = "";
Console.WriteLine($"[{nickname ?? "friend"}]");
Output
[]

To treat empty and blank values as missing too, test them with string.IsNullOrWhiteSpace(nickname) instead.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting ?? to replace an empty string"" is returned unchangedCheck with string.IsNullOrWhiteSpace
Using ?? on a value that can't be null, such as an interror CS0019: Operator '??' cannot be applied to operands of type 'int' and 'int'Use it on nullable values, such as int? or string?
Writing if (x == null) x = y; everywhereWorks, but repeats the variableWrite x ??= y;

Troubleshooting​

SymptomLikely causeFix
CS0019 with ??The left side can never be nullRemove ??, or make the type nullable
The fallback never appearsThe value is empty or blank, not nullTest with string.IsNullOrWhiteSpace

FAQ​

Is the fallback evaluated when it isn't used?​

No. The reference: "The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null." That matters when the fallback is a method call that does work.

What can go on the left of ??=?​

The reference: "The left-hand operand of the ??= operator must be a variable, a property, or an indexer element." It has to be something that can be assigned.

Conclusion​

value ?? fallback gives a value a replacement for null, and variable ??= fallback sets a variable only while it's null. Neither touches empty strings, and neither works on types that can never be null.

Additional Resources​