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

前端 未结 16 689
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 17:24

Let\'s say I have this enum:

[Flags]
enum Letters
{
     A = 1,
     B = 2,
     C = 4,
     AB = A | B,
     All = A | B | C,
}

To check i

16条回答
  •  猫巷女王i
    2020-11-29 17:51

    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
    }
    else
    {
        // None of them are enabled
    }
    

提交回复
热议问题