Enum中[Flags]特性

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);

 

posted @ 2021-06-25 15:30  WoodLeaf  阅读(238)  评论(0)    收藏  举报