C#

Copy Code

[Flags] public enum SomeOptions { FirstOption = 1, SecondOption = 2, ThirdOption = 4, FourthOption = 8, FifthOption = 16 // Each time the number doubles. }

This would not be suitable:

C#

Copy Code

public enum SomeOptions { FirstOption = 1, SecondOption = 2, ThirdOption = 3, FourthOption = 4, FifthOption = 5 // Just add 1 to the number each time. }

Using the "Flags" attribute just tells the compiler that you will be using this enum in the scenario you have described (combining various options). By doubling the numbers (1, 2, 4, 8, ...), you ensure that using the OR operator on them will produce valid results. The reason for that is how numbers are stored in binary. 1 is stored as "0001", 2 as "0010", 4 as "0100", 8 as "1000", and so on. If you combine 1 and 4, you get "0101" (i.e., the binary digits don't overlap).