-2

The intended purpose of the FlagsAttribute class is to allow enumerations to be used as flags...

[Flags]
enum Color
{
    None = 0,
    Red = 1,
    Green = 2,
    Blue = 4
}

Notice there the numerical values grow exponentially to allow combinations (i.e. 3 == Red | Green)

Say I have an enumeration with 100 different values...how do I accommodate this with respect that the largest value won't even fit in a ulong?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

1 Answers1

1

Use something other than an enum. A struct with multiple flags enums or even just ints inside it will work. You can make the fields private, and the lists of options as a bunch of static readonly fields. Just add what ever operators and methods you want and you should be good to go.

C#'s flags enums arn't very good at what they do anyway (HasFlag incurs an allocation for example: Why Enum's HasFlag method need boxing? ). You can implement your own equivalent methods more efficiently and generically when making your own alternatives to enum types.

struct Color
{
    public static readonly Color None = new Color(),
    public static readonly Color Red = new Color(1),
    public static readonly Color Green = new Color(2),
    public static readonly Color Blue = new Color(100)
    private int data1;
    private int data2;
    private Color(int channel){...}
}

It's worth noting that you likely don't need a 100 binary dimensional color space. For colors you wouldn't need flags enum, just a normal enum (if you want a set of predefined colors) or something like a struct with red green and blue values if you want any custom color.

CraigM
  • 389
  • 2
  • 5