TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
?? 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:
- If
Ais a nullable value type and an implicit conversion exists fromBto the underlying type ofA, the result type is the underlying type ofA. For example, evaluatingint? ?? intyields a result type ofint. - Otherwise, if an implicit conversion exists from
BtoA, the result type isA. - Otherwise, if an implicit conversion exists from
A(or its underlying type ifAis a nullable value type) toB, the result type isB. For example, evaluatingstring ?? objectyields a result type ofobject.
?? 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
Master C# with Deep Grasping Methodology!Learn More





