Kombinierte Antworten https://stackoverflow.com/a/8462/1037948 (Deklaration durch Bit-Shifting) und https://stackoverflow.com/a/9117/1037948 (unter Verwendung von Kombinationen in der Deklaration) können Sie frühere Werte per Bit-Shift verschieben, anstatt Zahlen zu verwenden. Ich empfehle das nicht unbedingt, sondern weise nur darauf hin, dass man das kann.
Anstatt:
[Flags]
public enum Options : byte
{
None = 0,
One = 1 << 0, // 1
Two = 1 << 1, // 2
Three = 1 << 2, // 4
Four = 1 << 3, // 8
// combinations
OneAndTwo = One | Two,
OneTwoAndThree = One | Two | Three,
}
Sie können erklären
[Flags]
public enum Options : byte
{
None = 0,
One = 1 << 0, // 1
// now that value 1 is available, start shifting from there
Two = One << 1, // 2
Three = Two << 1, // 4
Four = Three << 1, // 8
// same combinations
OneAndTwo = One | Two,
OneTwoAndThree = One | Two | Three,
}
Bestätigen Sie mit LinqPad:
foreach(var e in Enum.GetValues(typeof(Options))) {
string.Format("{0} = {1}", e.ToString(), (byte)e).Dump();
}
Ergebnisse in:
None = 0
One = 1
Two = 2
OneAndTwo = 3
Three = 4
OneTwoAndThree = 7
Four = 8