???? 和??• 運算子(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 和更早版本中,??運算子的左側運算元的類型必須是參考型別空數值型別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 開始,該要求替換為以下內容:????=運算子的左側運算元的類型不能為非空數值型別。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 開始,可以使用具有無約束類型參數的空合併運算子: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:

  • 在具有 null條件運算子的運算式中 ,可以使用??運算子提供替代運算式來計算,以防具有 null 條件操作的運算式的結果為nullIn 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
    
  • 當您使用空數值型別並且需要提供基礎數值型別的值時,使用??運算子指定要提供的值,以防空類型值為nullWhen 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");
    }
    

    上述範例中也會示範如何使用運算式主體成員定義屬性。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# 語言規範空合併運算子部分。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