C# Reference ยท Operators and Expressions
C# null-coalescing operators ?? and ??=
Reference for the C# null-coalescing operator ?? and null-coalescing assignment ??=: default values, lazy initialization, throw expressions and nullable types.
The null-coalescing operator ?? returns its left operand if that operand is not null; otherwise it evaluates and returns the right operand. It turns "use this value, or a fallback" into one short expression.
The null-coalescing assignment operator ??= assigns the right operand to the left one only when the left one is null. It is the idiomatic way to initialize something lazily or to apply a default to a variable in place.
string display = nickname ?? "anonymous"; cache ??= new Dictionary<string, int>();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
result = left ?? right;
target ??= value;
| Operator | Description |
|---|---|
a ?? b |
a if a is not null, otherwise b. Right-associative: a ?? b ?? c is a ?? (b ?? c). |
a ??= b |
If a is null, evaluate b and assign it to a; otherwise do nothing. The result of the expression is the final value of a. |
Returns: the left operand's value if it is not null, otherwise the right operand. For a nullable value type T? on the left, the result type is T when the right side is a T.
Errors: the left operand must be a reference type, a nullable value type or a type parameter known to be nullable; applying ?? to a plain int is a compile error. ??= needs an assignable left side. Neither operator treats an empty string or 0 as missing: only null.
Usage notes
- Short-circuits. The right operand runs only when the left is
null, so it is a safe place for expensive or side-effecting code (and forthrow). - Throw expressions.
x ?? throw new ...is the compact way to validate arguments and fields. Modern code also usesArgumentNullException.ThrowIfNull(x). - Nullable reference types. The compiler understands both operators: after
s ??= "x", the flow state ofsis "not null", ands ?? ""has a non-nullable type. - Not a falsy check. Unlike
||in some languages,"",0andfalseare valid values and are kept. Usestring.IsNullOrEmptyorIsNullOrWhiteSpacefor those. ??=is not thread-safe. Two threads may both seenulland both create a value. UseLazy<T>,LazyInitializeror a lock for shared state.- Property setters.
obj.Prop ??= xreads the property once and calls the setter only when it wasnull. - Combine with
?..order?.Customer?.Name ?? "unknown"walks a chain safely and supplies a fallback at the end; see null-conditional operators. - Overloading.
??and??=cannot be overloaded. - Performance: a single null check and branch.