Null Coalescing Operator
C# 5.0 in a Nutshell: The Definitive Reference by Definitive Reference Kindle Edition
by Joseph Albahari, Ben Albahari.
The ?? operator:
– can be used with both nullable types and reference types.
– says “If the operand is non-null, give it to me; otherwise, give me a default value.”
– equivalent to calling GetValueOrDefault with an explicit default value, except that the expression for the default value is never evaluated if the variable is not null.
1 2 3 4 |
int? x = null; int y = x ?? 5; // y is 5 |
1 2 3 4 |
int? a = null, b = 1, c = 2; Console.WriteLine(a ?? b ?? c); // 1 (first non-null value) |
Albahari & Albahari, C# 5.0 In a Nutshell: The Definitive Reference, 156-157.