条件运算符不能隐式转换(Conditional operator cannot cast implicitly?)

编程入门 行业动态 更新时间:2024-10-27 00:29:25
条件运算符不能隐式转换(Conditional operator cannot cast implicitly?)

我有点被这个小C#怪癖所困扰:

给定变量:

Boolean aBoolValue; Byte aByteValue;

以下编译:

if (aBoolValue) aByteValue = 1; else aByteValue = 0;

但这不会:

aByteValue = aBoolValue ? 1 : 0;

错误说:“不能将类型'int'隐式转换为'byte'。

当然,这个怪物会编译:

aByteValue = aBoolValue ? (byte)1 : (byte)0;

这里发生了什么?

编辑:

使用VS2008,C#3.5

I'm a little stumped by this little C# quirk:

Given variables:

Boolean aBoolValue; Byte aByteValue;

The following compiles:

if (aBoolValue) aByteValue = 1; else aByteValue = 0;

But this will not:

aByteValue = aBoolValue ? 1 : 0;

Error says: "Cannot implicitly convert type 'int' to 'byte'."

And of course, this monstrosity will compile:

aByteValue = aBoolValue ? (byte)1 : (byte)0;

What's going on here?

EDIT:

Using VS2008, C# 3.5

最满意答案

这是一个相当频繁的问题。

在C#中,我们几乎总是从内向外推理。 当你看到

x = y;

我们计算出什么是x的类型,y的类型以及y的类型是否与x兼容。 但是,当我们制定出y的类型时,我们不知道x的类型。

那是因为可能有多个x:

void M(int x) { } void M(string x) { } ... M(y); // y is assigned to either int x or string x depending on the type of y

我们需要能够找出表达式的类型, 而不必知道它被分配给什么。 键入信息流出表达式,而不是表达式。

要计算条件表达式的类型,我们计算出结果的类型和替代表达式,选择两个类型的更一般的,并且这将成为条件表达式的类型。 所以在你的例子中,条件表达式的类型是“int”,它不是一个常量(除非条件表达式是常量true或常量false)。 由于它不是常数,所以不能将其分配给字节; 编译器的原因只是从类型,而不是从值,当结果不是一个常量。

所有这些规则的例外是lambda表达式,其中类型信息从上下文流入到lambda中。 正确的逻辑是非常困难的。

This is a fairly frequently asked question.

In C#, we almost always reason from inside to outside. When you see

x = y;

we work out what is the type of x, what is the type of y, and whether the type of y is assignment compatible with x. But we do not use the fact that we know what the type of x is when we are working out the type of y.

That's because there might be more than one x:

void M(int x) { } void M(string x) { } ... M(y); // y is assigned to either int x or string x depending on the type of y

We need to be able to work out the type of an expression without knowing what it is being assigned to. Type information flows out of an expression, not into an expression.

To work out the type of the conditional expression, we work out the type of the consequence and the alternative expressions, pick the more general of the two types, and that becomes the type of the conditional expression. So in your example, the type of the conditional expression is "int", and it is not a constant (unless the condition expression is constant true or constant false). Since it is not a constant, you can't assign it to byte; the compiler reasons solely from the types, not from the values, when the result is not a constant.

The exception to all these rules is lambda expressions, where type information does flow from the context into the lambda. Getting that logic right was very difficult.

更多推荐

本文发布于:2023-07-30 23:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   条件   隐式   Conditional   cast

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!