C#中Enum有关[Flags]特性的作用以及用法:
[Flags]常用于做权限管理、位运算
[Flags]
enum Operation
{
insert = 0, //00001
delete = 1, //00010
select = 2, //00100
update = 4, //01000
other = 8 //10000
}
|:加法
Operation.update | Operation.other
01000 | 10000 = 11000
&:作比较
Operation.update & Operation.other;
01000 & 10000 = 00000
~:非
~Operation.update
~01000 =10111
&~:减法
Operation.update & ~ Operation.other;
01000 & ~ 10000 = 00000
01000 & 01111 = 01000
HasFlag的作用:
判断枚举值的包含关系
bool isflag = o.HasFlag(Operation.update);
原文:https://www.cnblogs.com/WoodLeaf/p/14931157.html