Null-Coalescing Operators
?? and ??= defaults.
Null-Coalescing Operators is a free C# Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
Supplying a Fallback
Often you want to use a value if it exists, or a default if it is null. The null-coalescing operator ?? does exactly this.
a ?? b means: use a if it is not null, otherwise use b.
string input = null;
string name = input ?? "Guest";
System.Console.WriteLine(name);How ?? Evaluates
The left side is evaluated first. If it is non-null, the right side is never evaluated at all.
This short-circuiting means an expensive fallback only runs when actually needed.
string cached = "data";
string value = cached ?? Compute();
System.Console.WriteLine(value);
// Compute() is never called hereWorks with Nullable Value Types
?? also unwraps nullable value types. int? maybe combined with ?? 0 produces a plain non-nullable int.
This is a clean way to convert from int? to int with a default.
int? maybe = null;
int sure = maybe ?? 0;
System.Console.WriteLine(sure);A Runnable Example
This program reads a possibly-null setting and falls back to a default. Run it to see the fallback in action.
using System;
class Program
{
static void Main()
{
string theme = null;
string active = theme ?? "light";
Console.WriteLine("Theme: " + active);
}
}Chaining ??
You can chain several fallbacks. The expression picks the first non-null value from left to right.
a ?? b ?? c tries a, then b, then finally c as the last resort.
string a = null;
string b = null;
string c = "default";
System.Console.WriteLine(a ?? b ?? c);The ??= Operator
The null-coalescing assignment ??= assigns the right side only if the left is currently null.
x ??= y is shorthand for if (x == null) x = y; and leaves x unchanged when it already has a value.
string config = null;
config ??= "default";
System.Console.WriteLine(config);
config ??= "ignored";
System.Console.WriteLine(config);Lazy Initialization with ??=
A common use of ??= is lazy initialization: create an object only the first time it is needed.
On later calls the field is already set, so ??= does nothing.
System.Collections.Generic.List<int> items = null;
items ??= new System.Collections.Generic.List<int>();
items.Add(1);
System.Console.WriteLine(items.Count);Combining ?. and ??
Pairing ?. with ?? is extremely common. The ?. avoids the exception, and ?? supplies a value when the chain is null.
user?.Name ?? "Unknown" reads the name safely, defaulting to "Unknown".
string s = null;
int length = s?.Length ?? -1;
System.Console.WriteLine(length);?? in a Full Program
Here both ?. and ?? work together to print a safe length. Run it with a null string to see the fallback.
using System;
class Program
{
static void Main()
{
string username = null;
string display = username ?? "anonymous";
Console.WriteLine(display);
Console.WriteLine(username?.Length ?? 0);
}
}?? Throwing on Null
The right side of ?? can be any expression, including throw. This makes ?? a compact way to fail fast on an unexpected null.
name ?? throw new ArgumentNullException(nameof(name)) returns the name, or throws if it is null.
string Validate(string name) =>
name ?? throw new System.ArgumentNullException(nameof(name));Right-Associative Behavior
?? is right-associative, so a ?? b ?? c groups as a ?? (b ?? c).
In practice this still means "first non-null wins", but it is good to know when reasoning about complex expressions.
int? a = null;
int? b = null;
int result = a ?? b ?? 99;
System.Console.WriteLine(result);Quick Check
Test your understanding of the null-coalescing operators.
Recap
?? returns its left side if non-null, otherwise the right side, and can be chained. ??= assigns only when the target is null, ideal for lazy init.
Combine them with ?. for safe, concise null handling, and even use ?? throw to fail fast.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Null-Coalescing Operators” lesson free?
Yes — the full text of “Null-Coalescing Operators” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “Null-Coalescing Operators”?
?? and ??= defaults. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Null-Coalescing Operators” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Nullable Value Types
- Null-Conditional Operator
- Null-Coalescing Operators
- Guarding Against Nulls