出于好奇:为什么我可以将0.0分配给枚举类型的变量,而不是1.0?看下面的代码:
public enum Foo
{
Bar,
Baz
}
class Program
{
static void Main()
{
Foo value1 = 0.0;
Foo value2 = 1.0; // This line does not compile
Foo value3 = 4.2; // This line does not compile
}
}
我认为数值类型和枚举值之间的转换只能通过强制转换进行吗?那就是我可以写Foo value2 = (Foo) 1.0;以便Main编译第2行。为什么0.0C#中的值有例外?
1.0文字分配给自定义枚举。