阅读量:0
在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);
通过以上方法,可以方便地定义和使用枚举类型,并对枚举成员进行操作。