?: 運算子 (C# 參考)?: operator (C# reference)

條件運算子 ?:(也稱為三元條件運算子)會評估布林運算式,並根據布林運算式是否評估為 truefalse,傳回兩個運算式其中之一的結果。The conditional operator ?:, also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false. 從 C# 7.2 開始,條件 ref 運算式會傳回兩個運算式其中之一結果的參考。Beginning with C# 7.2, the conditional ref expression returns the reference to the result of one of the two expressions.

條件運算子的語法如下:The syntax for the conditional operator is as follows:

condition ? consequent : alternative

condition 運算式必須評估為 truefalseThe condition expression must evaluate to true or false. 如果 condition 評估為 true,則會接著評估 consequent 運算式,且其結果會成為運算的結果。If condition evaluates to true, the consequent expression is evaluated, and its result becomes the result of the operation. 如果 condition 評估為 false,則會接著評估 alternative 運算式,且其結果會成為運算的結果。If condition evaluates to false, the alternative expression is evaluated, and its result becomes the result of the operation. 系統只會評估 consequentalternativeOnly consequent or alternative is evaluated.

consequentalternative 的型別必須相同,或是必須有從一個型別轉換成另一型別的隱含轉換。The type of consequent and alternative must be the same, or there must be an implicit conversion from one type to the other.

條件運算子是右向關聯運算子,亦即,以下形式的運算式The conditional operator is right-associative, that is, an expression of the form

a ? b : c ? d : e

會評估為is evaluated as

a ? b : (c ? d : e)

提示

您可以使用下列助憶鍵裝置來記住條件運算子的評估方式:You can use the following mnemonic device to remember how the conditional operator is evaluated:

is this condition true ? yes : no

下列範例示範條件運算子的用法:The following example demonstrates the usage of the conditional operator:

double sinc(double x) => x != 0.0 ? Math.Sin(x) / x : 1;

Console.WriteLine(sinc(0.1));
Console.WriteLine(sinc(0.0));
// Output:
// 0.998334166468282
// 1

條件 ref 運算式Conditional ref expression

從 C# 7.2 開始,您可以使用條件 ref 運算式來傳回兩個運算式其中之一結果的參考。Beginning with C# 7.2, you can use the conditional ref expression to return the reference to the result of one of the two expressions. 您可以將該參考指派給 ref 區域變數ref readonly 區域變數,或使用它作為參考傳回值或作為 ref 方法參數You can assign that reference to a ref local or ref readonly local variable, or use it as a reference return value or as a ref method parameter.

條件 ref 運算式的語法如下:The syntax for the conditional ref expression is as follows:

condition ? ref consequent : ref alternative

與原始條件運算子相同,條件 ref 運算式只會評估兩個運算式其中之一:consequentalternativeLike the original conditional operator, the conditional ref expression evaluates only one of the two expressions: either consequent or alternative.

就條件 ref 運算式而言,consequentalternative 的型別必須相同。In the case of the conditional ref expression, the type of consequent and alternative must be the same.

下列範例示範條件 ref 運算式的用法:The following example demonstrates the usage of the conditional ref expression:

var smallArray = new int[] { 1, 2, 3, 4, 5 };
var largeArray = new int[] { 10, 20, 30, 40, 50 };

int index = 7;
ref int refValue = ref ((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]);
refValue = 0;

index = 2;
((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]) = 100;

Console.WriteLine(string.Join(" ", smallArray));
Console.WriteLine(string.Join(" ", largeArray));
// Output:
// 1 2 100 4 5
// 10 20 0 40 50

條件運算子和 if..else 陳述式Conditional operator and an if..else statement

當您需要有條件地計算值時,使用條件運算子(而不是else 語句)可能會導致更簡潔的程式碼。Use of the conditional operator instead of an if-else statement might result in more concise code in cases when you need conditionally to compute a value. 下列範例示範兩種將整數分類為負值或非負值的方法:The following example demonstrates two ways to classify an integer as negative or nonnegative:

int input = new Random().Next(-5, 5);

string classify;
if (input >= 0)
{
    classify = "nonnegative";
}
else
{
    classify = "negative";
}

classify = (input >= 0) ? "nonnegative" : "negative";

運算子是否可多載Operator overloadability

使用者定義型別無法多載條件運算子。A user-defined type cannot overload the conditional operator.

C# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格條件運算子一節。For more information, see the Conditional operator section of the C# language specification.

如需條件 ref 運算式的詳細資訊,請參閱功能提案注意事項For more information about the conditional ref expression, see the feature proposal note.

請參閱See also