[英]What's the difference between these two enum [Flags] declarations (C#)
This is more a question I'm asking to understand rather than figure out a problem. 这是一个我要求理解而不是找出问题的问题。 Consider the following two: 考虑以下两点:
[Flags]
public enum Flags
{
NONE = 0x0,
PASSUPDATE = 0x1,
PASSRENDER = 0x2,
DELETE = 0x4,
ACCEPTINPUT = 0x8,
FADE_IN = 0x10,
FADE_OUT = 0x20,
FADE_OUT_COMPLETE = 0x40
}
[Flags]
public enum Flags
{
NONE = 0x0,
PASSUPDATE,
PASSRENDER,
DELETE,
ACCEPTINPUT,
FADE_IN ,
FADE_OUT,
FADE_OUT_COMPLETE
}
If I do bit checking on something using the latter enum there sometimes is overlap (I think something like DELETE
is interpreted as PASSUPDATE | PASSRENDER
, while in the first example each entry is independent of the other (ie DELETE
is only DELETE
and cannot be proven using a combination of a different set of flags). 如果我使用后一个枚举检查某些内容,有时会有重叠(我认为像DELETE
这样的东西被解释为PASSUPDATE | PASSRENDER
,而在第一个例子中,每个条目都独立于另一个(即DELETE
只是DELETE
,无法证明)使用一组不同的标志组合)。
Without explicit numbers, enums increment by 1 each time (even with [Flags]
specified), so you get: 如果没有显式数字,枚举每次都会增加1(即使指定了[Flags]
),所以你得到:
[Flags]
public enum Flags
{
NONE = 0x0,
PASSUPDATE, // = 1
PASSRENDER,// = 2
DELETE,// = 3
ACCEPTINPUT,// = 4
FADE_IN ,// = 5
FADE_OUT,// = 6
FADE_OUT_COMPLETE// = 7
}
which is simply not the numbers you wanted (and certainly isn't bitwise flags which are typically successive powers of 2) 这根本不是你想要的数字(当然也不是按位标志,通常是2的连续幂)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.