语法
[Flags]
public enum Permission
{
create = 1,
read = 2,
update = 4,
delete = 8,
}
注意事项
应用
使用
//初始化
WeekEnum week = (WeekEnum)127;
Console.WriteLine(week.ToString()); //输出 Sunday, Monday, Tuesday, Wendnesday, Thursday, Friday, Saturday
WeekEnum week = WeekEnum.Sunday;
//添加一个星期
week = week | WeekEnum.Monday;
Console.WriteLine(week.ToString()); // 输出Sunday, Monday
//去掉一个星期
week = week & ~WeekEnum.Sunday;
Console.WriteLine(week.ToString()); //输出 Monday
//判断是否有某个元素
obj.HasFlag() // 返回True False
string day = "monday";
WeekEnum today = (WeekEnum)Enum.Parse(typeof(WeekEnum), day, true);
// 最后一个参数标识字符串是否区分大小写
xiupublic enum Color : short
{
}
// 可以变化为 byte short int 等类型
public enum Suits
{
Spades,
Hearts,
Clubs,
Diamonds,
NumSuits
}
private static void Main(string[] args)
{
foreach (Suits suit in Enum.GetValues(typeof(Suits)))
{
Console.WriteLine((int)suit+ ":" + suit);
}
}
原文:https://blog.51cto.com/14200510/2353965