C#枚举的类型

参考文献:

1.https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/enum

2.https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/enumeration-types

 

枚举:

1.默认情况下,枚举中每个元素的基础类型都为 int

2.如果未为枚举器列表中的元素指定值,则值将自动按 1 递增。

3.已批准的枚举类型有 bytesbyteshortushortintuint, long 或 ulong

4.枚举数名称中不能含有空格。

5.枚举成员采用驼峰命名

6.若要在枚举上设置标志,请使用按位 OR 运算符,如以下示例所示:// Initialize with two flags using bitwise OR. meetingDays = Days.Tuesday | Days.Thursday;

7.若要确定是否设置了特定标志,请使用按位 AND 运算,如以下示例所示:// Test value of flags using bitwise AND. bool test = (meetingDays & Days.Thursday) == Days.Thursday;

8.使用 System.Enum 方法来发现和操作枚举值

 

[Flags]
enum Days
{
    None = 0x0,
    Sunday = 0x1,
    Monday = 0x2,
    Tuesday = 0x4,
    Wednesday = 0x8,
    Thursday = 0x10,
    Friday = 0x20,
    Saturday = 0x40
}
class MyClass
{
    Days meetingDays = Days.Tuesday | Days.Thursday;
}
// Initialize with two flags using bitwise OR.
meetingDays = Days.Tuesday | Days.Thursday;

// Set an additional flag using bitwise OR.
meetingDays = meetingDays | Days.Friday;

Console.WriteLine("Meeting days are {0}", meetingDays);
// Output: Meeting days are Tuesday, Thursday, Friday

// Remove a flag using bitwise XOR.
meetingDays = meetingDays ^ Days.Tuesday;
Console.WriteLine("Meeting days are {0}", meetingDays);
// Output: Meeting days are Thursday, Friday
// Test value of flags using bitwise AND.
bool test = (meetingDays & Days.Thursday) == Days.Thursday;
Console.WriteLine("Thursday {0} a meeting day.", test == true ? "is" : "is not");
// Output: Thursday is a meeting day.

 8.

string s = Enum.GetName(typeof(Day), 4);
Console.WriteLine(s);

Console.WriteLine("The values of the Day Enum are:");
foreach (int i in Enum.GetValues(typeof(Day)))
    Console.WriteLine(i);

Console.WriteLine("The names of the Day Enum are:");
foreach (string str in Enum.GetNames(typeof(Day)))
    Console.WriteLine(str);

 

转载于:https://www.cnblogs.com/jiangyan219/articles/11023764.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值