?? and ??= operators - null-coalescing operators - C# reference (original) (raw)
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. 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;
Console.WriteLine((numbers is null)); // expected: true
// if numbers is null, initialize it. Then, add 5 to numbers
(numbers ??= new List<int>()).Add(5);
Console.WriteLine(string.Join(" ", numbers)); // output: 5
Console.WriteLine((numbers is null)); // expected: false
Console.WriteLine((a is null)); // expected: true
Console.WriteLine((a ?? 3)); // expected: 3 since a is still null
// if a is null then assign 0 to a and add a to the list
numbers.Add(a ??= 0);
Console.WriteLine((a is null)); // expected: false
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.
The type of the left-hand operand of the ?? and ??= operators can't be a non-nullable value type. In particular, 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: NaN - When 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: -1 Use the Nullable.GetValueOrDefault() method if the value to be used when a nullable type value is null should be the default value of the underlying value type.
- You can use a throw expression 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.
- You can use the
??=operator to replace the code of the form
if (variable is null)
{
variable = expression;
} with the following code:
variable ??= expression; Operator overloadability
The operators ?? and ??= can't 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 Compound assignment section of the C# language specification.