Flags allow an enum value to contain many values. An enum type with the [Flags] attribute can have multiple constant values assigned to it. And it is still possible to test for these values in switches and if-statements.

Example. The [Flags] attribute on an enum allows you to assign multiple values to your enum at once. You can do this with bitwise manipulations, meaning you can store an enum with A and C set, not just A and not just C.

Note: This approach has benefits in some cases, but also problems. This next program uses enum Flags in many ways.

This example shows an enum with six bit flags in it. Note how the word [Flags] appears on top of the enum, with the square brackets. This is an attribute that tells .NET to allow you to assign the enum with bitwise operators.

Enum values. The values 0x0, 0x1, 0x2, 0x4 and so on indicate powers of two. In computer bits, powers of two contain one bit set, moving from the first bit to the final bit. You could use the decimal values, 0, 1, 2, 4, 8... instead.

The Main method here has several example steps. First it declares a new RenderType enum and assigns the RenderType.ContentPage value. This is the same as you would use a normal enum.

Next: The "|=" operator actually adds a flag to the enum, so the enum now contains two flag bits.

Also: You can use "|=" to add bits, while & will test bits without setting them.

Switch. The method Check is the final part of the example. It shows how to switch on enum flags. This is useful—it allows you to act on combinations of the flags in a constant-time expression.

Note: The "|" operator is used in the cases. This simply means the values are combined.

Bitwise AND, OR. Bitwise AND, which you use with &, returns a value with 1 in the targeted bit if both values contain the bit. You can therefore AND two values together and test the result for the target bit.

Bitwise OR, which you use with "|", returns 1 in the bit if either value has it set. This means it can be used in the switch, setting all bits in the "|" expressions. Examples are shown in the above program.

Discussion. When you are using enum flags, you are concerned about performance. The switch compiles to a jump table which is lots faster than if-else chains. I could not find other examples that use the flags in a switch.

Also, using bitwise flags on your enum has severe limitations. It can help improve certain code that has few flags that are often combined. But you cannot extend your enum flags nearly as much as a regular enum.

Therefore: If you need 100 enum values, or may need that many in the future, you should use separate integer enums.

Summary. Enum flags represent more complex constants and combinations. We used bitwise flags with enums in the C# language. The example shows how to assign, add, set, check, and switch on an enum with the [Flags] attribute.

Also: We looked at the flaws in this approach and how it can limit the development of your code.