http://www.cnblogs.com/fanwenxuan/archive/2007/10/16/926019.htmlhtml
枚舉是一個特定的常量集合組成的獨特類型
using System;
public enum TimeOfDay
{
Morning = 0,
Afternoon = 1,
Evening
}
class EnumExample
{
public static int Main()
{
//調用方法
WriteGreeting(TimeOfDay.Morning);
//獲取枚舉字符串
TimeOfDay time = TimeOfDay.Afternoon;
Console.WriteLine(time.ToString());
//獲取枚舉字符串的值
TimeOfDay time2 = (TimeOfDay) Enum.Parse(typeof(TimeOfDay), "Evening", true);
Console.WriteLine((int)time2);
// 遍歷全部的枚舉元素
Type time3=typeof(TimeOfDay);
foreach(string s in Enum.GetNames(time3))
{
Console.WriteLine(s);
}
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;
}
}
}
輸出結果:
Good Morning!
Afternoon
2
Morning
Afternonn
Eveninghtm