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

前端 未结 16 628
佛祖请我去吃肉
佛祖请我去吃肉 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:28

    There is HasFlag method in .NET 4 or higher.

    if(letter.HasFlag(Letters.AB))
    {
    }
    
    0 讨论(0)
  • 2020-11-29 17:30

    In .NET 4 you can use the Enum.HasFlag method :

    using System;
    
    [Flags] public enum Pet {
       None = 0,
       Dog = 1,
       Cat = 2,
       Bird = 4,
       Rabbit = 8,
       Other = 16
    }
    
    public class Example
    {
       public static void Main()
       {
          // Define three families: one without pets, one with dog + cat and one with a dog only
          Pet[] petsInFamilies = { Pet.None, Pet.Dog | Pet.Cat, Pet.Dog };
          int familiesWithoutPets = 0;
          int familiesWithDog = 0;
    
          foreach (Pet petsInFamily in petsInFamilies)
          {
             // Count families that have no pets. 
             if (petsInFamily.Equals(Pet.None))
                familiesWithoutPets++;
             // Of families with pets, count families that have a dog. 
             else if (petsInFamily.HasFlag(Pet.Dog))
                familiesWithDog++;
          }
          Console.WriteLine("{0} of {1} families in the sample have no pets.", 
                            familiesWithoutPets, petsInFamilies.Length);   
          Console.WriteLine("{0} of {1} families in the sample have a dog.", 
                            familiesWithDog, petsInFamilies.Length);   
       }
    }
    

    The example displays the following output:

    //       1 of 3 families in the sample have no pets. 
    //       2 of 3 families in the sample have a dog.
    
    0 讨论(0)
  • 2020-11-29 17:32
    if((int)letter != 0) { }
    
    0 讨论(0)
  • 2020-11-29 17:32

    Sorry, but i will show it in VB :)

       <Flags()> Public Enum Cnt As Integer
            None = 0
            One = 1
            Two = 2
            Three = 4
            Four = 8    
        End Enum
    
        Sub Test()
        Dim CntValue As New Cnt
        CntValue += Cnt.One
        CntValue += Cnt.Three
        Console.WriteLine(CntValue)
        End Sub
    

    CntValue = 5 So the enum contains 1 + 4

    0 讨论(0)
  • 2020-11-29 17:33

    I created a simple extension method that does not need a check on Enum types:

    public static bool HasAnyFlag(this Enum value, Enum flags)
    {
        return
            value != null && ((Convert.ToInt32(value) & Convert.ToInt32(flags)) != 0);
    }
    

    It also works on nullable enums. The standard HasFlag method does not, so I created an extension to cover that too.

    public static bool HasFlag(this Enum value, Enum flags)
    {
        int f = Convert.ToInt32(flags);
    
        return
            value != null && ((Convert.ToInt32(value) & f) == f);
    }
    

    A simple test:

    [Flags]
    enum Option
    {
        None = 0x00,
        One = 0x01,
        Two = 0x02,
        Three = One | Two,
        Four = 0x04
    }
    
    [TestMethod]
    public void HasAnyFlag()
    {
        Option o1 = Option.One;
        Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
        Assert.AreEqual(false, o1.HasFlag(Option.Three));
    
        o1 |= Option.Two;
        Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
        Assert.AreEqual(true, o1.HasFlag(Option.Three));
    }
    
    [TestMethod]
    public void HasAnyFlag_NullableEnum()
    {
        Option? o1 = Option.One;
        Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
        Assert.AreEqual(false, o1.HasFlag(Option.Three));
    
        o1 |= Option.Two;
        Assert.AreEqual(true, o1.HasAnyFlag(Option.Three));
        Assert.AreEqual(true, o1.HasFlag(Option.Three));
    }
    

    Enjoy!

    0 讨论(0)
  • 2020-11-29 17:34

    There are a lot of answers on here but I think the most idiomatic way to do this with Flags would be Letters.AB.HasFlag(letter) or (Letters.A | Letters.B).HasFlag(letter) if you didn't already have Letters.AB. letter.HasFlag(Letters.AB) only works if it has both.

    0 讨论(0)
提交回复
热议问题