第三操作员混淆(a = b?c:d) [英] Tertiary Operator Confusion ( a = b ? c : d)

查看:75
本文介绍了第三操作员混淆(a = b?c:d)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否是编译器错误。这一切都很有意义

但你会认为编译器会检查第三类运算符(c和d)的可能类型和c / d类型分配的变量

(a)。相反,它比较了可能不兼容的c和d的类型。看起来很傻。


int? i;

对象o;


//这没关系:

if(i.HasValue)

o = i;

else

o = DBNull.Value;


//这无法编译

//错误1无法确定条件表达式的类型因为

//''int?''和''System.DBNull'之间没有隐式转换'


o = i.HasValue?我:DBNull.Value;


//这些也因为同样的原因而失败


o = i == null? DBNull.Value:我;

o = i.HasValue?我:ABC;


//但这些作品


o = i.HasValue? (对象)i:DBNull.Value;

o = i.HasValue? (对象)i :(对象)DBNull.Value;

o = i.HasValue? i :(对象)DBNull.Value;

I don''t know if this is a compiler error or not. It all kind of makes sense
but you would think that the compiler would check the types of the possible
outcomes of the tertiary operator (c and d) against the type of the variable
being assigned (a). Instead it compares the types of c and d which may not
be compatible. Just seems silly.

int? i;
object o;

//this is ok:
if (i.HasValue)
o = i;
else
o = DBNull.Value;

//this fails to compile
//Error 1 Type of conditional expression cannot be determined because
//there is no implicit conversion between ''int?'' and ''System.DBNull''

o = i.HasValue ? i : DBNull.Value;

//these also fail for the same reason

o = i == null ? DBNull.Value: i;
o = i.HasValue ? i : "ABC";

//but these works

o = i.HasValue ? (object)i : DBNull.Value;
o = i.HasValue ? (object)i : (object)DBNull.Value;
o = i.HasValue ? i : (object)DBNull.Value;

推荐答案

一个观察。似乎编译器正在执行规则

,两个备选方案必须具有相同的类型。这对我来说很有意义

当你考虑到这样的高级表达式可用于

复杂的复合表达式,其中最终目标类型不是

用一个简单的赋值声明就可以了。


==============

Clay Burch

Syncfusion,Inc。

One observation. It does seem that the compiler is enforcing the rule
that both alternatives must have the same type. This makes sense to me
when you consider that such tertiary expressions can be used in
complicated compound expressions where the ultimate target type is not
as obvious as it might be with a simple assignment statement.

==============
Clay Burch
Syncfusion, Inc.




" ClayB" < cl *** @ syncfusion.comwrote in message

news:11 ********************** @ s48g2000cws.googlegr oups .com ...

"ClayB" <cl***@syncfusion.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...

一个观察结果。似乎编译器正在执行规则

,两个备选方案必须具有相同的类型。这对我来说很有意义

当你考虑到这样的高级表达式可用于

复杂的复合表达式,其中最终目标类型不是

一个简单的赋值语句可能很明显。
One observation. It does seem that the compiler is enforcing the rule
that both alternatives must have the same type. This makes sense to me
when you consider that such tertiary expressions can be used in
complicated compound expressions where the ultimate target type is not
as obvious as it might be with a simple assignment statement.



但.NET中的所有表达式都有一个共同类型,即对象。

可能当前错误应替换为警告,将

三元表达式解析为最近的公共超类型,它可能是对象的对象,或对象与某些接口集合的联合。编译器

然后可以确定随后使用哪个接口并自动生成演员。

However all expressions in .NET have a common type, which is object.
Probably the current error should be replaced with a warning, with the
ternary expression resolved as the nearest common supertype which may be
object, or the union of object with some set of interfaces. The compiler
could then determine which interface is subsequently used and automatically
generate the cast.


>

==============

Clay Burch

Syncfusion,Inc。
>
==============
Clay Burch
Syncfusion, Inc.



1月26日下午2:07,Ben Voigt < r ... @nospam.nospamwrote:
On Jan 26, 2:07 pm, "Ben Voigt" <r...@nospam.nospamwrote:

一个观察结果。似乎编译器正在执行规则

,两个备选方案必须具有相同的类型。这对我来说很有意义

当你考虑到这样的高级表达式可用于

复杂的复合表达式,其中最终目标类型不是

一个简单的赋值语句可能很明显。
One observation. It does seem that the compiler is enforcing the rule
that both alternatives must have the same type. This makes sense to me
when you consider that such tertiary expressions can be used in
complicated compound expressions where the ultimate target type is not
as obvious as it might be with a simple assignment statement.


但是.NET中的所有表达式都有一个共同的类型,即对象。

可能是当前的错误应替换为警告,并将

三元表达式解析为最近的公共超类型,它可能是对象的对象,或对象与某些接口集合的联合。编译器

然后可以确定随后使用哪个接口并自动

生成演员表。
However all expressions in .NET have a common type, which is object.
Probably the current error should be replaced with a warning, with the
ternary expression resolved as the nearest common supertype which may be
object, or the union of object with some set of interfaces. The compiler
could then determine which interface is subsequently used and automatically
generate the cast.



以下是实际使用的规则。就个人而言,我认为这是好的,因为它是:b / b
< quote>

第二和第三个操作数?:运算符控制条件表达式的类型

。设X和Y为第二个和第二个操作数的第二个和第二个操作数。然后,


*如果X和Y是相同的类型,那么这是

条件表达式的类型。

*否则,如果从X到Y存在隐式转换(第13.1节),

但不是从Y到X,那么Y是条件表达式的类型。

*否则,如果从Y到X存在隐式转换(第13.1节),

但不是从X到Y,则X是条件表达式的类型。

*否则,无法确定表达式类型,并且发生

编译时错误。

< / quote>


Jon

Here are the rules which are actually used. Personally, I think it''s
fine as it is:

<quote>
The second and third operands of the ?: operator control the type of
the conditional expression. Let X and Y be the types of the second and
third operands. Then,

*If X and Y are the same type, then this is the type of the
conditional expression.
*Otherwise, if an implicit conversion (§13.1) exists from X to Y,
but not from Y to X, then Y is the type of the conditional expression.
*Otherwise, if an implicit conversion (§13.1) exists from Y to X,
but not from X to Y, then X is the type of the conditional expression.
*Otherwise, no expression type can be determined, and a
compile-time error occurs.
</quote>

Jon


这篇关于第三操作员混淆(a = b?c:d)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆