(This is a duplicate, but itβs hard to find, so Iβm satisfied enough to provide another goal for future searches ...)
This is a null-coalescing operator. Essentially, it evaluates the first operand, and if the result is null (either a null reference or a null value for the NULL value type), then it evaluates the second operand. The result - no matter which operand is evaluated last, is effective.
Note that due to its associativity, you can write:
int? x = E1 ?? E2 ?? E3 ?? E4;
if E1 , E2 , E3 and E4 are all expressions of type int? - it starts with E1 and lasts until it finds a nonzero value.
The first operand must be a type with a null value, but the second operand may not be null, in which case the general type of the expression is not NULL. For example, suppose E4 is an expression of type int (but everyone else remains int? , then you can make x non-nullable:
int x = E1 ?? E2 ?? E3 ?? E4;
Jon skeet
source share