Skip to main content
The ?? operator, formally known as the null-coalescing operator, returns the value of its left-hand operand if it is not null; otherwise, it evaluates the right-hand operand and returns its result.

Technical Characteristics

Operand Constraints The left-hand operand must be a reference type, a nullable value type (Nullable<T> or T?), or, starting with C# 8.0, an unconstrained type parameter (T). Type Resolution The return type of the expression a ?? b (where a is of type A and b is of type B) is determined at compile-time by evaluating available implicit conversions in the following strict order of precedence:
  1. If A is a nullable value type and an implicit conversion exists from B to the underlying type of A, the result type is the underlying type of A. For example, evaluating int? ?? int yields a result type of int.
  2. Otherwise, if an implicit conversion exists from B to A, the result type is A.
  3. Otherwise, if an implicit conversion exists from A (or its underlying type if A is a nullable value type) to B, the result type is B. For example, evaluating string ?? object yields a result type of object.
Short-Circuit Evaluation The ?? operator utilizes short-circuit evaluation. The right-hand operand is evaluated strictly if and only if the left-hand operand evaluates to null. If the left-hand operand is non-null, the right-hand operand is bypassed entirely. Associativity and Evaluation Order The operator is right-associative. An expression chained with multiple null-coalescing operators is parsed and grouped from right to left (e.g., a ?? b ?? c is grouped as a ?? (b ?? c)). However, operand evaluation is strictly left-to-right. The left-most operand (a) is evaluated first; if it is not null, the entire right side (b ?? c) is short-circuited and never evaluated. Throw Expressions Since C# 7.0, the right-hand operand of the null-coalescing operator can be a throw expression. This allows the program to throw an exception directly during evaluation if the left-hand operand evaluates to null.

Syntax Visualization

Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More