A normal enum holds one value at a time. A flags enum holds any combination: a set of options packed into the bits of a single integer. Mark the enum with [Flags], give each member its own bit, and combine members with the bitwise OR operator |.
.NET uses this pattern all over the place: FileAttributes, RegexOptions and BindingFlags are all flags enums.
Defining a flags enum
[Flags]
public enum Colors
{
None = 0,
Yellow = 1 << 0, // 1
Green = 1 << 1, // 2
Red = 1 << 2, // 4
Blue = 1 << 3, // 8
Orange = 1 << 4, // 16
Black = 1 << 5, // 32
White = 1 << 6 // 64
}
Each value must be a power of two so that every member owns exactly one bit. Writing them as 1 << n makes that obvious and avoids typos; writing 1, 2, 4, 8, 16 by hand produces the same values. Laid out in binary, each member sets a different bit:
| Member | Value | Binary |
|---|---|---|
| Yellow | 1 | 0000001 |
| Green | 2 | 0000010 |
| Red | 4 | 0000100 |
| Blue | 8 | 0001000 |
| Orange | 16 | 0010000 |
| Black | 32 | 0100000 |
| White | 64 | 1000000 |
Include a None = 0 member. It is the value of an enum field that was never set, and it gives you a readable name for “no flags”.
Setting, clearing, toggling and testing
Colors theme = Colors.Yellow | Colors.Red;
Console.WriteLine(theme); // Yellow, Red
Console.WriteLine((int)theme); // 5
Console.WriteLine(theme.HasFlag(Colors.Red)); // True
Console.WriteLine((theme & Colors.Green) != 0); // False
theme |= Colors.Blue; // set a flag
theme &= ~Colors.Yellow; // clear a flag
theme ^= Colors.Black; // toggle a flag
Console.WriteLine(theme); // Red, Blue, Black
Testing works by masking. theme & Colors.Red keeps only the bits that are set in both values:
theme 0000101 (Yellow | Red)
Colors.Red 0000100
------- AND
result 0000100 not zero, so Red is set
HasFlag does the same check and reads better. It used to be slow, because it boxed both values; since .NET Core 2.1 the JIT compiles it down to the same AND and compare, so there is no reason to avoid it any more.
What [Flags] actually changes
The attribute does not change any values and does not enforce powers of two. What it changes is formatting and parsing. Without [Flags], (Colors.Yellow | Colors.Red).ToString() returns "5", because no single member has that value. With it, you get "Yellow, Red". Parsing works in the other direction:
Colors parsed = Enum.Parse<Colors>("Yellow, Red");
Console.WriteLine(parsed == (Colors.Yellow | Colors.Red)); // True
Listing the flags that are set
foreach (Colors color in Enum.GetValues<Colors>())
{
if (color != Colors.None && theme.HasFlag(color))
Console.WriteLine(color);
}
For the theme above, this prints Red, Blue and Black on separate lines. Enum.GetValues<T>() is available from .NET 5; on older versions use Enum.GetValues(typeof(Colors)).
Traps
HasFlag(None) is always true. Every value “contains” zero bits, so theme.HasFlag(Colors.None) returns true whatever theme is. That is why the loop above skips None. To check whether no flags are set, compare with == Colors.None.
Combined members. You can define shortcuts such as Warm = Yellow | Orange | Red. That is fine, but HasFlag(Warm) is only true when all three bits are set, and ToString() will print Warm instead of the individual names when the value matches exactly.
Running out of bits. An int-based enum has 32 bits. If you need more flags, declare the enum as : long or : ulong and shift a long: 1L << 40. Writing 1 << 40 on an int silently wraps around to 1 << 8.
Storing flags. Flags enums map well to a single integer column in a database, but the numbers then become part of your schema. Never renumber existing members; only add new bits.