我有这个枚举:
[Flags]
public enum MyEnum
{
NegativeValue = -1,
Value0 = 0,
Value1 = 2 ^ 1,
Value2 = 2 ^ 2,
Value3 = 2 ^ 3,
Value4 = 2 ^ 4
}
现在我想在这个枚举上使用一个开关:
public void SwitchThroughEnum(MyEnum myEnum)
{
switch (myEnum)
{
case MyEnum.NegativeValue:
break;
case MyEnum.Value0:
break;
case MyEnum.Value1:
break;
case MyEnum.Value2:
break;
case MyEnum.Value3:
break;
case MyEnum.Value4:
break;
default:
break;
}
}
但我无法编译它,因为 Visual Studio 告诉我“switch 语句包含多个标签值为‘0’的情况”。我不知道为什么会这样。
编辑:是否有可能以仅使用 1、2 等的幂的方式创建枚举?有时我有超过 30 个条目的枚举,计算和编写数字很费时间
有时我有超过 30 个条目的枚举,计算和编写数字很费时间
是的,只是位移。
[Flags]
public enum MyEnum
{
NegativeValue = -1,
Value0 = 0,
Value1 = 1,
Value2 = 2 << 0,
Value3 = 2 << 1,
Value4 = 2 << 2
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句