2013年5月24日 星期五

[C#] Enum用法

public enum EnumWeek
{
        Monday=1,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday = 0
}


///將字串轉換成Enum
var day1 = (EnumWeek)Enum.Parse(typeof(EnumWeek), "monday", true);
var day2 = (EnumWeek)Enum.Parse(typeof(EnumWeek), "tuesday", true);
var day7 = (EnumWeek)Enum.Parse(typeof(EnumWeek), "sunday", true);

//取得Enum的值(字串)
Console.WriteLine(day1.ToString());
Console.WriteLine(day2.ToString());
Console.WriteLine(day7.ToString());

//取得Enum的值(數字)
Console.WriteLine((int)day1);
Console.WriteLine((int)day2);
Console.WriteLine((int)day7);



Output :

Monday
Tuesday
Sunday
1
2
0

沒有留言:

張貼留言