为什么我不能通过单个隐式转换到枚举来打开类 [英] Why can't I switch on a class with a single implicit conversion to an enum

查看:31
本文介绍了为什么我不能通过单个隐式转换到枚举来打开类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么对枚举值的单个隐式转换与转换为系统类型时的工作方式不同.我看不出任何技术原因,但也许有人比我更聪明,可以为我提供一些启发.

I am wondering why it is that a single implicit conversion to an enum value doesn't work the same way it would if the conversion were to a system type. I can't see any technical reason however maybe someone smarter than I can shed some light for me.

以下内容无法编译,预期整数类型的值"无法将类型 'Test.En' 隐式转换为 'Test.Foo".

The followoing fails to compile with, "A value of an integral type expected" and "Cannot implicitly convert type 'Test.En' to 'Test.Foo".

void test1 (){
    Foo f = new Foo();

    switch (f)         // Comment this line to compile
    //switch ((En)f)   // Uncomment this line to compile
    {
        case En.One:
            break;
    }
}


//////////////////////////////////////////////////////////////////

public enum En
{
    One,
    Two,
    Three,
}

public class Foo
{
    En _myEn;

    public static implicit operator En(Foo f)
    {
        return f._myEn;
    }
}

从规范中

编辑:

switch 语句的控制类型由 switch 表达式建立.如果 switch 表达式的类型是 sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type,那么这就是 switch 的控制类型陈述.否则,从 switch 表达式的类型到以下可能的控制类型之一,必须存在一个用户定义的隐式转换(第 6.4 节):sbyte、byte、short、ushort、int、uint、long、ulong、字符,字符串.如果不存在这样的隐式转换,或者存在多个这样的隐式转换,则会发生编译时错误.

The governing type of a switch statement is established by the switch expression. If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type, then that is the governing type of the switch statement. Otherwise, exactly one user-defined implicit conversion (§6.4) must exist from the type of the switch expression to one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string. If no such implicit conversion exists, or if more than one such implicit conversion exists, a compile-time error occurs.

为了澄清这个问题,为什么枚举类型没有包含在允许的用户定义的隐式转换列表中?

To Clarify the question, why is an enum-type not included with the list of allowed user-defined implicit conversions?

推荐答案

语言设计说明存档没有为这个决定提供理由.这是不幸的,因为决定被改变了.如您所见,设计随着时间的推移而演变:

The language design notes archive does not provide a justification for this decision. This is unfortunate, since the decision was changed. As you can see, the design evolved over time:

1999 年 5 月 26 日的注释:

Notes from May 26th, 1999:

允许哪些类型作为switch 语句的参数?整数类型,包括字符、枚举类型,布尔.C# 也允许类型可以隐式和明确转换为其中之一上述类型.(如果有多重隐式转换,则其不明确和编译时错误发生.)我们不确定我们是否是否支持字符串.

What types are allowed in as the argument to a switch statement? integral types including char, enum types, bool. C# also permits types that can be implicitly and unambiguously converted to one of the aforementioned types. (If there are multiple implicit conversion, then its ambiguous and a compile-time error occurs.) We're not sure whether we want to support string or not.

1999 年 6 月 7 日:

June 7th, 1999:

我们讨论了在字符串上启用开关论据.我们认为这是一个很好的功能——语言可以增加价值通过使这种常见情况更容易写,以及额外的复杂性对于用户来说非常低.

We discussed enabling switch on string arguments. We think this is a good feature – the language can add value by making this common case easier to write, and the additional complexity for the user is very low.

1999 年 12 月 20 日:

December 20th, 1999:

打开电源是违法的bool 类型的表达式.这是合法的打开一个表达式整数类型或字符串类型.这是打开 a 的表达式是合法的类型只有一个隐式转换为整数类型或字符串类型.

It is illegal to switch on an expression of type bool. It is legal to switch on an expression of an integral type or string type. It is legal to switch on an expression of a type that has exactly one implicit conversion to an integral type or string type.

在这里,我们第一次出现了有问题的规则.枚举似乎已经消失了.为什么不使用用户定义的隐式转换来枚举?这只是一个疏忽吗?设计师没有记录他们的想法.

Here we have the first occurence of the rule in question. Enums seem to have disappeared. And why not use user-defined implicit conversions to enum? Was this simply an oversight? The designers did not record their thoughts.

请注意,第一句话不是我们实施的.我不清楚为什么实施者的做法与设计委员会的建议相反.这在几年后的笔记中再次出现:

Note that the first sentence is NOT what we implemented. It is unclear to me why the implementors did the opposite of what the design committee recommended. This comes up again in the notes several years later:

2003 年 8 月 13 日:

August 13, 2003:

编译器允许打开 bool.不想记录并添加它到语言.不想删除出于兼容性原因.决定了默默继续支持switch布尔值.

The compiler allows switch on bool. Don’t want to document this and add it to the language. Don’t want to remove it for compatibility reasons. Decided to silently continue to support switch on bool.

我认为这很愚蠢;当我们制作带有注释的 C# 3.0 规范印刷版时,我将 bool(和 bool?)添加到了合法管理类型列表中.

I decided that this was silly; when we produced the annotated print edition of the C# 3.0 specification, I added bool (and bool?) to the list of legal governing types.

简而言之:整件事有点乱.我不知道为什么枚举先进入,然后退出,然后半进半出.这可能仍是未知之谜之一.

In short: the whole thing is a bit of a mess. I have no idea why enums were in, then out, then half-in-half-out. This might have to remain one of the Mysteries of the Unknown.

这篇关于为什么我不能通过单个隐式转换到枚举来打开类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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