?: 运算符(C# 参考)?: operator (C# reference)

条件运算符 (?:) 通常被称为三元条件运算符,用于计算 Boolean 表达式,并根据 Boolean 表达式的计算结果为 true 还是 false 来返回计算两个表达式其中之一的结果。The conditional operator ?:, commonly known as the ternary conditional operator, evaluates a Boolean expression, and returns the result of evaluating one of 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 localref readonly local 变量,或将它用作引用返回值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

有关详细信息,请参阅功能建议说明For more information, see the feature proposal note.

条件运算符和 if..else 语句Conditional operator and an if..else statement

需要根据条件计算值时,在 if-else 语句中使用条件运算符可以使代码更简洁。Use of the conditional operator over 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

无法重载条件运算符。The conditional operator cannot be overloaded.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范条件运算符部分。For more information, see the Conditional operator section of the C# language specification.

请参阅See also