程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 為枚舉(enum)類型添加的備注屬性,並且獲取枚舉的屬性值

為枚舉(enum)類型添加的備注屬性,並且獲取枚舉的屬性值

編輯:C#入門知識

枚舉類型(也稱為枚舉)是非常有意思的的一種值類型,它不僅可以提高程序的可讀性,而且可以減少因業務改變而導致的程序改動。

 另外枚舉類型是強類型,接受方法必須有一個相同的匹配參數,否則將會出錯。

 好,廢話少說,至於枚舉類型(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>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved