???? 和 ??= 运算符(C# 参考)and ??= operators (C# reference)

如果左操作数的值不为 null,则 null 合并运算符 ?? 返回该值;否则,它会计算右操作数并返回其结果。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. 如果左操作数的计算结果为非 null,则 ?? 运算符不会计算其右操作数。The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.

C# 8.0 及更高版本中可使用空合并赋值运算符 ??=,该运算符仅在左侧操作数的求值结果为 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. 如果左操作数的计算结果为非 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.

在 C# 7.3 及更早版本中,?? 运算符左操作数的类型必须是引用类型可以为 null 的值类型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. 从 C# 8.0 版本开始,该要求替换为以下内容:????= 运算符的左操作数的类型必须是可以为 null 的值类型。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. 特别是从 C# 8.0 开始,可以使用具有无约束类型参数的 null 合并运算符: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);
}

null 合并运算符是右结合运算符。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:

  • 在包含 null 条件运算符 ?. 和 ?[] 的表达式中,当包含 null 条件运算的表达式结果为 null 时,可以使用 ?? 运算符来提供替代表达式用于求值: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:

    double SumNumbers(List<double[]> setsOfNumbers, int indexOfSetToSum)
    {
        return setsOfNumbers?[indexOfSetToSum]?.Sum() ?? double.NaN;
    }
    
    var sum = SumNumbers(null, 0);
    Console.WriteLine(sum);  // output: NaN
    
  • 当使用可以为 null 值类型并且需要提供基础值类型的值时,可以使用 ?? 运算符指定当可以为 null 的类型的值为 null 时要提供的值: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:

    int? a = null;
    int b = a ?? -1;
    Console.WriteLine(b);  // output: -1
    

    如果可以为 null 的类型的值为 null 时要使用的值应为基础值类型的默认值,请使用 Nullable<T>.GetValueOrDefault() 方法。Use the Nullable<T>.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.

  • 从 C# 7.0 开始,可以使用 throw 表达式作为 ?? 运算符的右操作数,以使参数检查代码更简洁:Beginning with C# 7.0, 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");
    }
    

    前面的示例还演示了如何使用 expression-bodied 成员来定义属性。The preceding example also demonstrates how to use expression-bodied members to define a property.

  • 从 C# 8.0 开始,可以使用 ??= 运算符将这样的代码Beginning with C# 8.0, 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 ??= cannot be overloaded.

C# 语言规范C# language specification

有关 ?? 运算符的详细信息,请参阅 C# 语言规范null 合并运算符部分。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