为什么不同枚举类型之间的操作允许在另一个枚举声明而不是其他地方? [英] Why are operations between different enum types allowed in another enum declaration but not elsewhere?

查看:242
本文介绍了为什么不同枚举类型之间的操作允许在另一个枚举声明而不是其他地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#编译器允许在另一个枚举类型声明不同枚举类型之间的操作,像这样的:

The C# compiler allows operations between different enum types in another enum type declaration, like this:

public enum VerticalAnchors
{
    Top=1,
    Mid=2,
    Bot=4
}

public enum HorizontalAnchors
{
    Lef=8,
    Mid=16,
    Rig=32
}

public enum VisualAnchors
{
    TopLef = VerticalAnchors.Top | HorizontalAnchors.Lef,
    TopMid = VerticalAnchors.Top | HorizontalAnchors.Mid,
    TopRig = VerticalAnchors.Top | HorizontalAnchors.Rig,
    MidLef = VerticalAnchors.Mid | HorizontalAnchors.Lef,
    MidMid = VerticalAnchors.Mid | HorizontalAnchors.Mid,
    MidRig = VerticalAnchors.Mid | HorizontalAnchors.Rig,
    BotLef = VerticalAnchors.Bot | HorizontalAnchors.Lef,
    BotMid = VerticalAnchors.Bot | HorizontalAnchors.Mid,
    BotRig = VerticalAnchors.Bot | HorizontalAnchors.Rig
}

但禁止他们里面的方法,code,即操作:

but forbids them inside method code, i.e. the operation:

VerticalAnchors.Top | HorizontalAnchors.Lef;

标记与此错误是:

Is flagged with this error:

运算符|不能被应用到类型'VerticalAnchors'的操作数和'Horizo​​ntalAnchors'

Operator '|' cannot be applied to operands of type 'VerticalAnchors' and 'HorizontalAnchors'.

有一个解决办法,当然是:

There's a workaround, of course:

(int)VerticalAnchors.Top | (int)HorizontalAnchors.Lef

我很好奇这个编译器行为。为什么在另一个枚举声明允许不同枚举类型之间的操作,但是不能在其他地方?

I am curious about this compiler behaviour. Why are operations between different enum types allowed in another enum declaration but not elsewhere?

推荐答案

这实际上不是在的规格据我可以告诉。也有一些是相关的:

It is actually not in the spec as far as I can tell. There is something related:

如果枚举成员的声明中具有恒定-EX pression   初始化,那不断前pression值,隐含   转换为枚举的基础类型,是关联的值   枚举成员。

If the declaration of the enum member has a constant-expression initializer, the value of that constant expression, implicitly converted to the underlying type of the enum, is the associated value of the enum member.

尽管 VerticalAnchors.Top和放大器; Horizo​​ntalAnchors.Lef 的类型 VerticalAnchors 可以隐式转换为 VisualA​​nchors 。但是,这并不能解释为什么不断前pression本身支持隐式转换无处不在。

Although VerticalAnchors.Top & HorizontalAnchors.Lef has type VerticalAnchors it can be implicitly converted to VisualAnchors. But this does not explain why the constant expression itself supports implicit conversions everywhere.

事实上,它明确地似乎是规范:

Actually, it appears explicitly to be against the spec:

恒EX pressions 的编译时计算使用相同的   除了规则运行时评价的非恒定EX pressions,   其中,运行时评价将抛出一个异常,编译时   评估导致发生编译时错误。

The compile-time evaluation of constant expressions uses the same rules as run-time evaluation of non-constant expressions, except that where run-time evaluation would have thrown an exception, compile-time evaluation causes a compile-time error to occur.

如果我没有错过什么,该规范不仅不会允许这种明确的,它不允许它。根据这一假设,这将是一个编译器错误。

If I didn't miss something, the spec not only does not allows this explicitly, it disallows it. Under that assumption it would be a compiler bug.

这篇关于为什么不同枚举类型之间的操作允许在另一个枚举声明而不是其他地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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