How to Use the Null-Coalescing Operators ?? and ??= in C#
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
<result> = <value> ?? <fallback>;
<variable> ??= <fallback>;
| Part | What 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
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

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
dotnet run main.cs
friend
Lee
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:
string? nickname = "";
Console.WriteLine($"[{nickname ?? "friend"}]");
[]
To treat empty and blank values as missing too, test them with string.IsNullOrWhiteSpace(nickname) instead.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting ?? to replace an empty string | "" is returned unchanged | Check with string.IsNullOrWhiteSpace |
Using ?? on a value that can't be null, such as an int | error 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; everywhere | Works, but repeats the variable | Write x ??= y; |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS0019 with ?? | The left side can never be null | Remove ??, or make the type nullable |
| The fallback never appears | The value is empty or blank, not null | Test 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
- ?? and ??= operators, on Microsoft Learn.
