Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (i.e. DELETE is only DELETE and cannot be proven using a combination of a different set of flags).

like image 795
meds Avatar asked Mar 20 '26 16:03

meds


1 Answers

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)

like image 55
Marc Gravell Avatar answered Mar 23 '26 05:03

Marc Gravell