重新把c#编程技术过一遍2(枚举)
时间:2010-12-13 来源:肉丝
下面是一个简单的例子:
public enum TimeOfDay
{
Morning =0,
Afternoon=1,
Evening =2,
}
class EnumExamaple
{
public static int Main()
{
WriteGreeting(TimeOfDay.Afternoon);
return 0;
}
static void WriteGreeting(TimeOfDay timeOfDay)
{
switch (timeOfDay)
{
case TimeOfDay.Morning :
Console.WriteLine("good morning");
break ;
case TimeOfDay.Afternoon:
Console.WriteLine("good afternoon");
break;
case TimeOfDay.Evening:
Console.WriteLine("good evening");
break;
default :
Console.WriteLine("hello");
break;
}
}
}
*可以获取枚举字段的字符串表示
TimeOfDay time = TimeOfDay.Afternoon;
Console.WriteLine(time.ToString());
*将字符串变为枚举,然后获取枚举值
TimeOfDay time = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "afternoon", true);
Console.WriteLine((int)time);