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,
// ...