# Using Expressions in C#

- 7.12 The Null Coalescing Operator
- 7.13 Conditional Operator
- 7.14 Anonymous Function Expressions
- 7.15 Query Expressions
- 7.16 Assignment Operators
- 7.17 Expressions
- 7.18 Constant Expressions

*The C# Programming Language*, along with colleagues Eric Lippert, Chris Sells, Fritz Onion, Joseph Albahari, and Don Box, discuss expressions in C#, including anonymous function expressions and query expressions.

## 7.12 The Null Coalescing Operator

The `??` operator is called the null coalescing operator.

null-coalescing-expression:conditional-or-expressionconditional-or-expression ?? null-coalescing-expression

A null coalescing expression of the form `a ?? b` requires `a` to be of a nullable type or reference type. If `a` is non-null, the result of `a ?? b` is `a`; otherwise, the result is `b`. The operation evaluates `b` only if `a` is null.

The null coalescing operator is right-associative, meaning that operations are grouped from right to left. For example, an expression of the form `a ?? b ?? c` is evaluated as `a ?? (b ?? c)`. In general terms, an expression of the form `E _{1} ?? E_{2} ??` ...

`?? E`returns the first of the operands that is non-null, or

_{N}`null`if all operands are null.

The type of the expression `a ?? b` depends on which implicit conversions are available between the types of the operands. In order of preference, the type of `a ?? b` is `A _{0}`,

`A`, or

`B`, where

`A`is the type of

`a`,

`B`is the type of

`b`(provided that

`b`has a type), and

`A`is the underlying type of

_{0}`A`if

`A`is a nullable type, or

`A`otherwise. Specifically,

`a ?? b`is processed as follows:

- If
`A`is not a nullable type or a reference type, a compile-time error occurs. - If
`A`is a nullable type and an implicit conversion exists from`b`to`A`, the result type is_{0}`A`. At runtime,_{0}`a`is evaluated first. If`a`is not null,`a`is unwrapped to type`A`, and this becomes the result. Otherwise,_{0}`b`is evaluated and converted to type`A`, and this becomes the result._{0} - Otherwise, if an implicit conversion exists from
`b`to`A`, the result type is`A`. At runtime,`a`is evaluated first. If`a`is not null,`a`becomes the result. Otherwise,`b`is evaluated and converted to type`A`, and this becomes the result. - Otherwise, if
`b`has a type`B`and an implicit conversion exists from`A`to_{0}`B`, the result type is`B`. At runtime,`a`is evaluated first. If`a`is not null,`a`is unwrapped to type`A`(unless_{0}`A`and`A`are the same type) and converted to type_{0}`B`, and this becomes the result. Otherwise,`b`is evaluated and becomes the result. - Otherwise,
`a`and`b`are incompatible, and a compile-time error occurs.