枚舉類型(也稱為枚舉)是非常有意思的的一種值類型,它不僅可以提高程序的可讀性,而且可以減少因業務改變而導致的程序改動。
另外枚舉類型是強類型,接受方法必須有一個相同的匹配參數,否則將會出錯。
好,廢話少說,至於枚舉類型(emun)的特性,幾乎會編程的人都懂的,偶沒功力在這裡長篇大論。
下面是一個例子,偶還是按官方的寫法,以星期為例子:
代碼
class Program
{
static void Main(string[] args)
{
Console.Write(GetDays(Days.Saturday));
Console.Read();
}
static string GetDays(Days _days)
{
string d = string.Empty;
switch (_days)
{
case Days.Monday: d = "Today is Monday."; break;
case Days.Tuesday: d = "Today is Tuesday."; break;
case Days.Wednesday: d = "Today is Wednesday."; break;
case Days.Thursday: d = "Today is Thursday."; break;
case Days.Friday: d = "Today is Friday."; break; ;
case Days.Saturday: d = "Today is Saturday."; break;
case Days.Sunday: d = "Today is Sunday."; break;
}
return d;
}
}
public enum Days
{
/// <summary>
/// this is sunday
/// </summary>
Sunday = 7,
/// <summary>
/// this is monday
/// </summary>
Monday = 1,
/// <summary>
/// this is tuesday
/// </summary>
Tuesday = 2,
/// <summary>