Search code examples
c#enum-flags

What is the convention for defining values for enum flags?


Let's say I have following code:

[Flags]
enum MyFlags
{
    None = 0,
    A = 1,
    B = 2,
    C = 4,
    D = 8,
    E = 16,
    // ...
}

This is obviously not going to be optimal when the amount of flags grow very large. And by optimal, mean readable, not fast or memory saving.

For combined flags, such as

AB = 3

we can easily use

AB = A | B

instead, which is more readable.
And for combining all flags

All = A | B | C | ...

it would be more favorable to use

All = ~None

instead, even if we don't make full use of all 32/64 bits available.

But what about regular values?

Between

E = 16
E = 0b10000
E = 0x10
E = 1 << 4

or other ways I haven't thought of, which would be best suited for a large amount of flags?

Or in other words, what is the (agreed upon) convention for setting values for flags in C#?

Let us assume for the sake of argument, that the values will not be aligned, so the code might very well look like this

None = 0,
Apple = 1,
Banana = 2,
StrangeFruit = Apple | Banana,
Cherry = 4,
Date = 8,
Elderberry = 16,
// ...

Solution

  • For regular values, I like 1 << n: after you've taken the (short) time to understand it, it's very easy to see what's going on, hard to mess up, and requires no hex/binary conversion/thinking.

    [Flags]
    enum MyFlags
    {
        None = 0,
        A = 1 << 0,
        B = 1 << 1,
        C = 1 << 2,
        D = 1 << 3,
        E = 1 << 4,
        // ...
        Lastly = 1 << 31,
    }
    

    As far as an actual defined convention, I don't think one exists. MS's Enum Design guidelines says

    √ DO use powers of two for the flag enum values so they can be freely combined using the bitwise OR operation.

    but does not specify how to write this in your source (those are language-agnostic guidelines; what might be good in C# might not even work in another .Net language).