简体   繁体   English

C#中的三元运算符

[英]Ternary Operator in C#

Can anyone please explain to me what happens behind the scenes when you use ternary operator? 任何人都可以向我解释当你使用三元运算符时幕后会发生什么? does this line of code: 这行代码:

string str = 1 == 1 ? "abc" : "def";

is generated as a simple if / else statement? 是作为一个简单的if / else语句生成的? Consider the following: 考虑以下:

class A
{
}

class B : A
{
}

class C : A
{
}

Now using ternary expression as follows: 现在使用三元表达式如下:

A a1 = 1 == 1 ? new B() : new C();

this doesn't even compile with this error: 这甚至没有使用此错误编译:

Type of conditional expression cannot be determined because there is no implicit conversion between 'ConsoleApp1.B' and 'ConsoleApp2.C' 无法确定条件表达式的类型,因为“ConsoleApp1.B”和“ConsoleApp2.C”之间没有隐式转换

Can anyone shed light on this one? 任何人都可以阐明这个吗?

The type of the conditional operator expression is required to be either the type of the second operand or the type of the third operand. 类型条件运算符表达的需要是或者第二操作数的类型或者第三操作数的类型。 So one of those must be convertible to the other. 所以其中一个必须可以转换为另一个。

In your case, they're not convertible to each other - but both convertible to a third type (A). 在你的情况下,它们不能相互转换 - 但都可以转换为第三种类型(A)。 That isn't considered by the compiler, but you can force it: 编译器不考虑这一点,但您可以强制它:

A a1 = 1 == 1 ? new B() : (A) new C();

or 要么

A a1 = 1 == 1 ? (A) new B() : new C();

See section 7.14 of the C# 4 spec for more details. 有关详细信息,请参阅C#4规范的第7.14节。

An extract from msdn ?Operator 来自msdn?Operator的摘录

If condition is true, first expression is evaluated and becomes the result; 如果condition为true,则计算第一个表达式并成为结果; if false, the second expression is evaluated and becomes the result. 如果为false,则计算第二个表达式并成为结果。 Only one of two expressions is ever evaluated. 只评估了两个表达式中的一个。

Its pretty explicit. 它很明确。

And your error is pretty explicit too, you are trying to assign a B to a C ... But no cast is available, so error ... Pretty simple 你的错误也是非常明确的,你试图将一个B分配给一个C ...但是没有可用的强制转换,所以错误...非常简单

The conditional operator will effectively use the type of the first expression for the second according to whether there is a conversion - and doesn't take into account bases (otherwise it would just always go to object allowing this: ? "hello" : 10 ). 根据是否存在转换,条件运算符将有效地使用第一个表达式的类型 - 并且不考虑基数(否则它将总是转到object允许这样: ? "hello" : 10 ) 。

In this case, the compiler is correct - there is no conversion between the two types. 在这种情况下,编译器是正确的 - 两种类型之间没有转换。 Add a cast, however on the first one - and it'll compile - (A)new B() . 添加一个演员,但是在第一个 - 并且它将编译 - (A)new B()

Its pretty explicit. 它很明确。

And your error is pretty explicit too, you are trying to assign a B to a C ... But no cast is available, so error ... Pretty simple 你的错误也是非常明确的,你试图将一个B分配给一个C ...但是没有可用的强制转换,所以错误...非常简单

Not relevant at all. 根本不相关。

B and C derives from A. B和C来自A.

The expression is: 表达式是:

A a1 = 1 == 1 ? A1 = 1 == 1? new B() : new C(); new B():new C();

Both expressions return type that derives from A 两个表达式都返回从A派生的类型

Just the compiler looks at the expressions of the ?: operator, and don't care what is the type of variable a1 (left side of the expression)... The reason for such implementation is very interesting... 只是编译器查看?:运算符的表达式,并不关心变量a1的类型(表达式的左侧)......这样实现的原因非常有趣......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM