开发者

What's the difference between these two enum [Flags] declarations (C#)

开发者 https://www.devze.com 2023-03-07 10:05 出处:网络
This is more a question I\'m asking to understand rather than figure 开发者_如何学Cout a problem. Consider the following two:

This is more a question I'm asking to understand rather than figure 开发者_如何学Cout 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 (i.e. DELETE is only DELETE and cannot be proven using a combination of a different set of flags).


Without explicit numbers, enums increment by 1 each time (even with [Flags] specified), so you get:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消