?? and ??= operators (C# reference)
The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
Available in C# 8.0 and later, the null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
List<int> numbers = null;
int? a = null;
(numbers ??= new List<int>()).Add(5);
Console.WriteLine(string.Join(" ", numbers)); // output: 5
numbers.Add(a ??= 0);
Console.WriteLine(string.Join(" ", numbers)); // output: 5 0
Console.WriteLine(a); // output: 0
The left-hand operand of the ??= operator must be a variable, a property, or an indexer element.
In C# 7.3 and earlier, the type of the left-hand operand of the ?? operator must be either a reference type or a nullable value type. Beginning with C# 8.0, that requirement is replaced with the following: the type of the left-hand operand of the ?? and ??= operators cannot be a non-nullable value type. In particular, beginning with C# 8.0, you can use the null-coalescing operators with unconstrained type parameters:
private static void Display<T>(T a, T backup)
{
Console.WriteLine(a ?? backup);
}
The null-coalescing operators are right-associative. That is, expressions of the form
a ?? b ?? c
d ??= e ??= f
are evaluated as
a ?? (b ?? c)
d ??= (e ??= f)
Examples
The ?? and ??= operators can be useful in the following scenarios:
In expressions with the null-conditional operators ?. and ?[], you can use the
??operator to provide an alternative expression to evaluate in case the result of the expression with null-conditional operations isnull:double SumNumbers(List<double[]> setsOfNumbers, int indexOfSetToSum) { return setsOfNumbers?[indexOfSetToSum]?.Sum() ?? double.NaN; } var sum = SumNumbers(null, 0); Console.WriteLine(sum); // output: NaNWhen you work with nullable value types and need to provide a value of an underlying value type, use the
??operator to specify the value to provide in case a nullable type value isnull:int? a = null; int b = a ?? -1; Console.WriteLine(b); // output: -1Use the Nullable<T>.GetValueOrDefault() method if the value to be used when a nullable type value is
nullshould be the default value of the underlying value type.Beginning with C# 7.0, you can use a
throwexpression as the right-hand operand of the??operator to make the argument-checking code more concise:public string Name { get => name; set => name = value ?? throw new ArgumentNullException(nameof(value), "Name cannot be null"); }The preceding example also demonstrates how to use expression-bodied members to define a property.
Beginning with C# 8.0, you can use the
??=operator to replace the code of the formif (variable is null) { variable = expression; }with the following code:
variable ??= expression;
Operator overloadability
The operators ?? and ??= cannot be overloaded.
C# language specification
For more information about the ?? operator, see The null coalescing operator section of the C# language specification.
For more information about the ??= operator, see the feature proposal note.