假设我有这样一个enum:

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

为了检查AB是否被设置,我可以这样做:

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

有没有一种比下面更简单的方法来检查是否设置了一个组合标志常量的任何标志?

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

例如,可以将&与其他东西交换吗?


当前回答

如何

if ((letter & Letters.AB) > 0)

?

其他回答

你可以在enum上使用这个扩展方法,用于任何类型的enum:

public static bool IsSingle(this Enum value)
{
    var items = Enum.GetValues(value.GetType());
    var counter = 0;
    foreach (var item in items)
    {
        if (value.HasFlag((Enum)item))
        {
            counter++;
        }
        if (counter > 1)
        {
            return false;
        }
    }
    return true;
}

你可以检查这个值是否不为零。

if ((Int32)(letter & Letters.AB) != 0) { }

但是我认为引入一个值为0的新枚举值并与该枚举值进行比较是更好的解决方案(如果可能的话,因为您必须能够修改枚举)。

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

if (letter != Letters.None) { }

更新

看错了问题——修正了第一个建议,忽略第二个建议。

为了检查AB是否被设置,我可以这样做: if((字母和字母。ab) ==字母。ab) 有没有一种比下面更简单的方法来检查是否设置了一个组合标志常量的任何标志?

这将检查是否设置了A和B,并忽略是否设置了任何其他标志。

if((字母和字母。a) ==字母。||(字母和字母。b) ==字母。b)

这将检查是否设置了A或B,并忽略是否设置了任何其他标志。

这可以简化为:

if(letter & Letters.AB)

这是二进制运算的C;应用到c#应该很简单:

enum {
     A = 1,
     B = 2,
     C = 4,
     AB = A | B,
     All = AB | C,
};

int flags = A|C;

bool anything_and_a = flags & A;

bool only_a = (flags == A);

bool a_and_or_c_and_anything_else = flags & (A|C);

bool both_ac_and_anything_else = (flags & (A|C)) == (A|C);

bool only_a_and_c = (flags == (A|C));

顺便提一下,问题示例中变量的命名是单数字母“letter”,这可能暗示它只代表一个字母;示例代码清楚地表明,它是一组可能的字母,并且允许多个值,因此可以考虑将变量重命名为“letters”。

if((int)letter != 0) { }

在. net 4中,您可以使用Enum。HasFlag方法:

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);   
   }
}

使用实例显示如下信息:

//       1 of 3 families in the sample have no pets. 
//       2 of 3 families in the sample have a dog.