为什么C#3允许将文字零(0)隐式转换为任何枚举?

Seb*_*ood 9 c# c#-3.0

C#3(Visual Studio 2008)引入了对语言的重大更改(http://msdn.microsoft.com/en-us/library/cc713578.aspx,请参阅更改12),允许将任何文字零隐式转换为一个枚举.这在很多方面都很奇怪.有谁知道为什么这是规范的一部分?为什么不是文字?还是七个?为什么零特殊?并且它使一些非常违反直觉的重载决策选择.例如.

function string F(object o) { return o.ToString(); }
function string F(DbType t) { return t.ToString(); }
int i = 0;
F((long)0) == "String" // would have been "0" in VS 2005
F(0) == "String"
F(i) == "0"
Run Code Online (Sandbox Code Playgroud)

非常混乱,故意引入了对语言的重大改变.有任何想法吗?

Jar*_*Par 10

C#始终允许将文字0隐式转换为任何枚举值.更改的是规则如何应用于其他常量表达式.它被设置为更加一致,并允许任何计算结果为0的常量表达式可以隐式转换为枚举.

您提供的示例在所有C#版本中产生相同的行为.

以下是更改行为的示例(直接来自链接文档)

public enum E
{
    Zero = 0,
    One = 1,
} 

class A
{
    public static A(string s, object o)
    { System.Console.WriteLine("{0} => A(object)", s); } 

    public static A(string s, E e)
    { System.Console.WriteLine("{0} => A(Enum E)", s); }

    static void Main()
    {
        A a1 = new A("0", 0);
        A a3 = new A("(int) E.Zero", (int) E.Zero);
    }
}
Run Code Online (Sandbox Code Playgroud)

Visual C#2005输出:

0 => A(Enum E)
(int) E.Zero => A(object)

Visual C#2008输出:

0 => A(Enum E)
(int) E.Zero => A(Enum E)