按位枚举(flags)的使用
按位枚举是为标记相互包含而使用的,每个成员被赋给一个唯一的按位值。
用按位或运算符(|)来组合按位标记。用按位和运算符(&)来确定一个标记的存在。
从组合状态中去掉一个元素用& (~)。
示例代码:
用按位或运算符(|)来组合按位标记。用按位和运算符(&)来确定一个标记的存在。
从组合状态中去掉一个元素用& (~)。
示例代码:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](/Images/OutliningIndicators/None.gif)
5
namespace ConsoleApplication1
6
{
7
class Program
8
{
9
[Flags]
10
public enum Contribution
11
{
12
Pension=0x01,
13
ProfitSharing=0x02,
14
CreditBureau=0x04,
15
SavingsPlan=0x08,
16
All = Pension | ProfitSharing | CreditBureau | SavingsPlan
17
}
18
public class Employee
19
{
20
private Contribution prop_contributions;
21
public Contribution contributions
22
{
23
get { return prop_contributions; }
24
set { prop_contributions = value; }
25
}
26
}
27![](/Images/OutliningIndicators/InBlock.gif)
28
public class Starter
29
{
30
public static void Main()
31
{
32
Employee bob = new Employee();
33
bob.contributions = Contribution.ProfitSharing | Contribution.CreditBureau;
34
if ((bob.contributions & Contribution.ProfitSharing) == Contribution.ProfitSharing)
35
{
36
Console.WriteLine("Bob enrolled in profit sharing");
37
Console.WriteLine(bob.contributions);
38
bob.contributions = bob.contributions & (~Contribution.ProfitSharing);
39
Console.WriteLine(bob.contributions);
40
}
41
}
42
}
43
}
44
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
44
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)