Enum.HasFlag(Enum) Methode

Definition

Bestimmt, ob ein oder mehrere Bitfelder in der aktuellen Instanz festgelegt werden.Determines whether one or more bit fields are set in the current instance.

public:
 bool HasFlag(Enum ^ flag);
public bool HasFlag (Enum flag);
member this.HasFlag : Enum -> bool
Public Function HasFlag (flag As Enum) As Boolean

Parameter

flag
Enum

Ein Enumerationswert.An enumeration value.

Gibt zurück

true, wenn das in flag festgelegte Bitfeld bzw. die Bitfelder auch in der aktuellen Instanz festgelegt werden, andernfalls false.true if the bit field or bit fields that are set in flag are also set in the current instance; otherwise, false.

Ausnahmen

flag weist einen anderen Typ als die aktuelle Instanz auf.flag is a different type than the current instance.

Beispiele

Im folgenden Beispiel wird eine ItemsOrdered Enumeration definiert, die Kategorien von Elementen widerspiegelt, die ein Kunde in einem Restaurant bestellen kann.The following example defines an ItemsOrdered enumeration that reflects categories of items that a customer can order in a restaurant. Im Beispiel wird getestet, ob der Kunde sowohl eine Entrée als auch ein Getränk bestellt hat.The example tests whether the customer has ordered both an entrée and a beverage.

using System;

[Flags] public enum DinnerItems {
   None = 0,
   Entree = 1,
   Appetizer = 2,
   Side = 4,
   Dessert = 8,
   Beverage = 16, 
   BarBeverage = 32
}

public class Example
{
   public static void Main()
   {
      DinnerItems myOrder = DinnerItems.Appetizer | DinnerItems.Entree |
                            DinnerItems.Beverage | DinnerItems.Dessert;
      DinnerItems flagValue = DinnerItems.Entree | DinnerItems.Beverage;
      Console.WriteLine("{0} includes {1}: {2}", 
                        myOrder, flagValue, myOrder.HasFlag(flagValue));
   }
}
// The example displays the following output:
//    Entree, Appetizer, Dessert, Beverage includes Entree, Beverage: True
<Flags> Public Enum DinnerItems As Integer
   None = 0
   Entree = 1
   Appetizer = 2
   Side = 4
   Dessert = 8
   Beverage = 16 
   BarBeverage = 32
End Enum

Module Example
   Public Sub Main()
      Dim myOrder As DinnerItems = DinnerItems.Appetizer Or DinnerItems.Entree Or
                                   DinnerItems.Beverage Or DinnerItems.Dessert
      Dim flagValue As DinnerItems = DinnerItems.Entree Or DinnerItems.Beverage
      Console.WriteLine("{0} includes {1}: {2}", 
                        myOrder, flagValue, myOrder.HasFlag(flagValue))
   End Sub
End Module
' The example displays the following output:
'    Entree, Appetizer, Dessert, Beverage includes Entree, Beverage: True

Hinweise

Die HasFlag -Methode gibt das Ergebnis des folgenden booleschen Ausdrucks zurück.The HasFlag method returns the result of the following Boolean expression.

thisInstance And flag = flag   

Wenn der zugrunde liegende Wert flag von 0 (null) ist true, gibt die Methode zurück.If the underlying value of flag is zero, the method returns true. Wenn dieses Verhalten nicht erwünscht ist, können Sie die Equals -Methode verwenden, um auf Gleichheit mit NULL zu prüfen und nur dann aufzurufen flag HasFlag , wenn der zugrunde liegende Wert von ungleich 0 (null) ist, wie im folgenden Beispiel veranschaulicht.If this behavior is not desirable, you can use the Equals method to test for equality with zero and call HasFlag only if the underlying value of flag is non-zero, as the following example illustrates.

using System;

[Flags] public enum Pets {
   None = 0,
   Dog = 1,
   Cat = 2,
   Bird = 4,
   Rabbit = 8,
   Other = 16
}

public class Example
{
   public static void Main()
   {
      Pets[] petsInFamilies = { Pets.None, Pets.Dog | Pets.Cat, Pets.Dog };
      int familiesWithoutPets = 0;
      int familiesWithDog = 0;

      foreach (var petsInFamily in petsInFamilies)
      {
         // Count families that have no pets.
         if (petsInFamily.Equals(Pets.None))
            familiesWithoutPets++;
         // Of families with pets, count families that have a dog.
         else if (petsInFamily.HasFlag(Pets.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.
<Flags> Public Enum Pets
   None = 0
   Dog = 1
   Cat = 2
   Bird = 4
   Rabbit = 8
   Other = 16
End Enum

Module Example
   Public Sub Main()
      Dim petsInFamilies() As Pets = { Pets.None, Pets.Dog Or Pets.Cat, Pets.Dog }
      Dim familiesWithoutPets As Integer
      Dim familiesWithDog As Integer
      
      For Each petsInFamily In petsInFamilies
         ' Count the number of families that have no pets.
         If petsInFamily.Equals(Pets.None) Then
            familiesWithoutPets += 1 
        ' Of families that have pets, count the number of families with a dog.
         Else If petsInFamily.HasFlag(Pets.Dog) Then
            familiesWithDog += 1
         End If
      Next
      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)   
   End Sub
End Module
' 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.

Die HasFlag -Methode ist für die Verwendung mit Enumerationstypen konzipiert, die FlagsAttribute mit dem-Attribut markiert sind, und kann verwendet werden, um zu bestimmen, ob mehrere Bitfelder festgelegt sind.The HasFlag method is designed to be used with enumeration types that are marked with the FlagsAttribute attribute and can be used to determine whether multiple bit fields are set. Für Enumerationstypen, die nicht mit FlagsAttribute dem-Attribut markiert sind Equals , wird entweder CompareTo die-Methode oder die-Methode aufgerufen.For enumeration types that are not marked with the FlagsAttribute attribute, call either the Equals method or the CompareTo method.

Gilt für:

Siehe auch