Null-Coalescing Operators
?? and ??= defaults.
Null-Coalescing Operators is a free C# Academy lesson on CoddyKit — 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
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, and the C# Academy course includes 4 lessons in total. 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.
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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
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