Enum.HasFlag Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Determines whether one or more bit fields are set in the current instance.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function HasFlag ( _
    flag As Enum _
) As Boolean
[SecuritySafeCriticalAttribute]
public bool HasFlag(
    Enum flag
)

Parameters

Return Value

Type: System.Boolean
true if the bit field or bit fields that are set in flag are also set in the current instance; otherwise, false.

Exceptions

Exception Condition
ArgumentException

flag is a different type than the current instance.

Remarks

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

thisInstance And flag = flag 

If the underlying value of flag is zero, the method returns true. 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.

<Flags()> Public Enum Pet
   None = 0
   Dog = 1
   Cat = 2
   Bird = 4
   Rabbit = 8
   Other = 16
End Enum

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim petsInFamilies() As Pet = {Pet.None, Pet.Dog Or Pet.Cat, Pet.Dog}
      Dim familiesWithoutPets As Integer
      Dim familiesWithDog As Integer

      For Each petsInFamily In petsInFamilies
         ' Count the number of families without pets.
         If petsInFamily.Equals(Pet.None) Then
            familiesWithoutPets += 1
         ' Of families with a pet, count the number of families with a dog.
         Else If petsInFamily.HasFlag(Pet.Dog) Then
            familiesWithDog += 1
         End If
      Next
      outputBlock.Text += String.Format("{0} of {1} families in the sample have no pets." & vbCrLf, _
                                        familiesWithDog, petsInFamilies.Length)
      outputBlock.Text += String.Format("{0} of {1} families in the sample have a dog." & vbCrLf, _
                                        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.
using System;

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

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Pet[] petsInFamilies = { Pet.None, Pet.Dog | Pet.Cat, Pet.Dog };
      int familiesWithoutPets = 0;
      int familiesWithDog = 0;

      foreach (Pet petsInFamily in petsInFamilies)
      {
         // Count the number of families that have no pets.
         if (petsInFamily.Equals(Pet.None))
            familiesWithoutPets++;
      // Of families with pets, count the number of families with a dog.
         else if (petsInFamily.HasFlag(Pet.Dog))
            familiesWithDog++;
      }
      outputBlock.Text += String.Format("{0} of {1} families in the sample have no pets.",
                        familiesWithoutPets, petsInFamilies.Length) + Environment.NewLine;
      outputBlock.Text += String.Format("{0} of {1} families in the sample have a dog.",
                        familiesWithDog, petsInFamilies.Length) + Environment.NewLine;
   }
}
// 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. For enumeration types that are not marked with the FlagsAttribute attribute, call either the Equals method or the CompareTo method.

Examples

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.

<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 Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim myOrder As DinnerItems = DinnerItems.Appetizer Or DinnerItems.Entree Or _
                                   DinnerItems.Beverage Or DinnerItems.Dessert
      Dim flagValue As DinnerItems = DinnerItems.Entree Or DinnerItems.Beverage
      outputBlock.Text += String.Format("{0} includes {1}: {2}" & vbCrLf, _
                        myOrder, flagValue, myOrder.HasFlag(flagValue))
   End Sub
End Module
' The example displays the following output:
'    Entree, Appetizer, Dessert, Beverage includes Entree, Beverage: True
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 Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      DinnerItems myOrder = DinnerItems.Appetizer | DinnerItems.Entree |
                            DinnerItems.Beverage | DinnerItems.Dessert;
      DinnerItems flagValue = DinnerItems.Entree | DinnerItems.Beverage;
      outputBlock.Text += String.Format("{0} includes {1}: {2}",
                        myOrder, flagValue, myOrder.HasFlag(flagValue)) + "\n";
   }
}
// The example displays the following output:
//    Entree, Appetizer, Dessert, Beverage includes Entree, Beverage: True

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.