Skip to content

Latest commit

 

History

History
96 lines (65 loc) · 4.59 KB

null-coalescing-operator.md

File metadata and controls

96 lines (65 loc) · 4.59 KB
title description ms.date f1_keywords helpviewer_keywords ms.assetid
?? and ??= operators - null-coalescing operators
The `??` and `??=` operators are the C# null-coalescing operators. They return the value of the left-hand operand if it isn't null. Otherwise, they return the value of the right-hand operand
11/28/2022
??_CSharpKeyword
??=_CSharpKeyword
null-coalescing operator [C#]
?? operator [C#]
null-coalescing assignment [C#]
??= operator [C#]
088b1f0d-c1af-4fe1-b4b8-196fd5ea9132

?? and ??= operators - the null-coalescing operators

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.

[!code-csharpnull-coalescing assignment]

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:

[!code-csharpunconstrained type parameter]

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 is null:

    [!code-csharp-interactivewith null-conditional]

  • 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 is null:

    [!code-csharp-interactivewith nullable types]

    Use the xref:System.Nullable%601.GetValueOrDefault?displayProperty=nameWithType 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:

    [!code-csharpwith throw expression]

    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 feature proposal note.

See also