在C#中,枚舉可以通過以下方式定義和使用:
enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
DaysOfWeek today = DaysOfWeek.Friday;
Console.WriteLine("Today is " + today);
int dayValue = (int)DaysOfWeek.Friday;
Console.WriteLine("Friday is " + dayValue);
DaysOfWeek tomorrow = (DaysOfWeek)(((int)today + 1) % 7);
Console.WriteLine("Tomorrow is " + tomorrow);
通過以上方法,可以方便地定義和使用枚舉類型,并對枚舉成員進行操作。