Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Enum.HasFlag-Methode (Enum)

.NET Framework (current version)
 

Bestimmt, ob ein oder mehrere Bitfelder in der aktuellen Instanz festgelegt werden.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public bool HasFlag(
	Enum flag
)

Parameter

flag

Ein Enumerationswert.

Rückgabewert

Type: System.Boolean

true, wenn das in flag festgelegte Bitfeld bzw. die Bitfelder auch in der aktuellen Instanz festgelegt werden, andernfalls false.

Exception Condition
ArgumentException

flag is a different type than the current instance.

The HasFlag method returns the result of the following Boolean expression.

thisInstance And flag = flag 

Wenn der zugrunde liegende Wert von flag 0 ist, gibt die Methode true zurück. 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.

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.For enumeration types that are not marked with the FlagsAttribute attribute, call either the Equals method or the CompareTo method.

The following example defines an ItemsOrdered enumeration that reflects categories of items that a customer can order in a restaurant.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

Universal Windows Platform
Verfügbar seit 4.5
.NET Framework
Verfügbar seit 4.0
Portable Class Library
Unterstützt in: portable .NET platforms
Silverlight
Verfügbar seit 4.0
Windows Phone Silverlight
Verfügbar seit 8.0
Windows Phone
Verfügbar seit 8.1
Zurück zum Anfang
Anzeigen: