How to use the FlagsAttribute flag

The developer of my project completed the following enumeration

[Flags] public enum Permissions { Overview = 1, Detail = 3, Edit = 7, Delete = 31, Block = 39, // Requires Edit = 7, and It own location = 32. Therefore 7 + 32 = 39. Unblock = 71, // Requires Edit = 7, and It own location = 64. Therefore 7 + 64 = 71. All = int.MaxValue } 

Now, as you can see, he did, for example, Details = 3 . The reason he did this is because the Details (which should have been 2) also include a review (2 + 1 = 3).

I always thought that the way to do this is to use powers of 2 in the enumeration and do some operations and do outside the enumeration. What's going on here?

+4
source share
2 answers

There is nothing wrong with defining combinations in an enumeration to make life easier for enumeration users. However, this is probably a short-sighted solution (inflexible and difficult to understand code) decision to leave aside the various options of power 2 that make up the combination.

+6
source

Documents recommend defining labeled enumeration constants in powers of two, but there is no restriction on defining labeled enumeration constants with any possible values.

Determine the enumeration constants by the powers of two, i.e. 1, 2, 4, 8, etc. This means that individual flags in combined enumeration constants do not overlap.

+5
source

All Articles