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

前端 未结 16 632
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  攒了一身酷
    2020-11-29 17:38

    If you can use .NET 4 or higher than use HasFlag() method

    examples

    letter.HasFlag(Letters.A | Letters.B) // both A and B must be set
    

    same as

    letter.HasFlag(Letters.AB)
    

提交回复
热议问题