标记枚举
时间:2011-05-17 来源:NineTyNine_LP
class Program
{
static void Main(string[] args)
{
TestA a = new TestA();
// 组合多个枚举值
a.Type = CellType.Type1 | CellType.Type2; // 0000 0011
Console.WriteLine(a.Type);
// 判断是否包含某个枚举值
//0000 0011 & 0000 0001 = 0000 0001
if ((a.Type & CellType.Type1) == CellType.Type1)
{
Console.WriteLine("含有类型1");
}
// 删去某个枚举值
// 0000 0011 & ( ~ 0000 0001 ) = 0000 0011 & 1111 1110 = 0000 0010
a.Type = a.Type & (~CellType.Type1);
Console.WriteLine(a.Type);
Console.Read();
}
}
public class TestA
{
public CellType Type
{
get;
set;
}
}
[Flags]
public enum CellType
{
/*
* 以十六进制表示,每个枚举对应的值都是2的整数倍
*/
Type1 = 0x01, // 0000 0001
Type2 = 0x02, // 0000 0010
Type3 = 0x04, // 0000 0100
Type4 = 0x08, // 0000 1000
Type5 = 0x10 // 0001 0000
}
相关阅读 更多 +










