Search code examples
c#.netenumsenum-flags

Is it ok to add an "All" item to a Flags enum?


If I have an enum with multiple values which can be present at the same time, I create a Flags enum:

[Flags]
public enum Foo
{
    None = 0,
    A = 1,
    B = 2,
    C = 4,
    D = 8
}

If I now want to pass the fact that every value is set I would have to do something like this:

Bar bar = new Bar(Foo.A | Foo.B | Foo.C | Foo.D);

Would it be considered bad practice/harmful/blasphemy to add an additional element All?

    All = 15

This would save some space and time when there are a lot of values and passing them all is a common scenario.

As far as I understand the mechanics of flags enums (=bitfields), this should work. Are there any side effects I am missing or other reasons why you should not do that?


Solution

  • You can do that. But maybe you shoudn't use the value 15, but

    All = A | B | C | D