#212 – Using Several Null-Coalescing (??) Operators in the Same Expression
January 15, 2011 Leave a comment
You can create an expression that contains more than one null-coalescing (??) operator. Because the ?? operator is left associative, the expression will be evaluated from left to right.
This means that:
int result = i ?? j ?? k ?? 0; // i, j and k are all of type int?
is equivalent to:
int result = ((i ?? j) ?? k) ?? 0;
The end result of chaining ?? operators together is that the result of the final expression is–the value of the first non-null operand. In our example, if i and k were null, but j had an integer value, result would be assigned the value of j.