Svish Svish - 4 months ago 23
C# Question

How to check if any flags of a flag combination are set?

Let's say I have this enum:

enum Letters
A = 1,
B = 2,
C = 4,
AB = A | B,
All = A | B | C,

To check if for example
is set I can do this:

if((letter & Letters.AB) == Letters.AB)

Is there a simpler way to check if any of the flags of a combined flag constant are set than the following?

if((letter & Letters.A) == Letters.A || (letter & Letters.B) == Letters.B)

Could you for example swap the
with something?

Not too stable when it comes to binary stuff like this...


If you want to know if letter has any of the letters in AB you must use the AND & operator. Something like:

if ((letter & Letters.AB) != 0)
    // Some flag (A,B or both) is enabled
    // None of them are enabled