Defining bit field enums
Many times we design our special kind of bit field to manage bitwise operations on enums. Here a simple example:
[
Flags]
public enum ConsoleModiefiers
{
Alt = 2,
Control = 4,
Shift = 8
}
When the number of values becomes higher, we have a little readibility problem with the value for each field. A possible alternative is to use the left-shift operator
[Flags]
public enum ConsoleModiefiers
{
Alt = 0x0001 << 1,
Control = 0x0001 << 2,
Shift = 0x0001 << 3
}
In term of performance we don't pay any penalties since the IL code generated by C# compiler is exactly the same, but our code is probably more clear.