Skip to content
Tutorials Center
My learning

Topics

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.

C#
string display = nickname ?? "anonymous";
cache ??= new Dictionary<string, int>();

Try it

C# Defaults with ??

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.

C# Lazy initialization with ??=

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

C#
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 for throw).
  • Throw expressions. x ?? throw new ... is the compact way to validate arguments and fields. Modern code also uses ArgumentNullException.ThrowIfNull(x).
  • Nullable reference types. The compiler understands both operators: after s ??= "x", the flow state of s is "not null", and s ?? "" has a non-nullable type.
  • Not a falsy check. Unlike || in some languages, "", 0 and false are valid values and are kept. Use string.IsNullOrEmpty or IsNullOrWhiteSpace for those.
  • ??= is not thread-safe. Two threads may both see null and both create a value. Use Lazy<T>, LazyInitializer or a lock for shared state.
  • Property setters. obj.Prop ??= x reads the property once and calls the setter only when it was null.
  • 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.