隐式枚举转换允许把十进制整数0 转换成任何枚举类型,对应其它的整数则不存在这种隐式转换,下面用一个例子来具体说明本问题:
using System;
enum Weekday{
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
};
class Test
{
public static void Main()
{
Weekday day;
day = 0;
Console.WriteLine(day);
}
}
程序的输出是 0
注意:把 day = 0 改写为 day = 1 编译器就会给出下面的错误:
Can not implictly convert type 'int' to type 'enum'
如果使用我们需要显示的转换 int x = (int)Weekday.Tuesday; int y = (int)Weekday.Friday;