C#中枚举用关键字enum定义,和定义类一样,将成员放在大括号内
public enum TimeOfDay
{
Monday=0,
Tuesday,
Wednesday=5,
Thursday=4,
Friday,
Saturday,
Sunday
}
TimeOfDay time=TimeOfDay.Monday;
Console.WriteLine(time.ToString());//将会输出字符串Monday
Console.WriteLine((int)time);//将会输出0
另外,还可以从字符串中获取枚举值:
TimeOfDay time2 = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "Tuesday", true);
Console.WriteLine((int)time2);//expected value is 1
TimeOfDay time3 = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "Tuesday_k", true);
//will throws a Exception.
阅读(1465) | 评论(0) | 转发(0) |