다음을 통해 공유


Enum.HasFlag(Enum) 메서드

정의

현재 인스턴스에 하나 이상의 비트 필드가 설정되어 있는지를 확인합니다.

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

매개 변수

flag
Enum

열거형 값입니다.

반환

Boolean

flag에 설정되어 있는 비트 필드가 현재 인스턴스에도 설정되어 있으면 true이고, 그렇지 않으면 false입니다.

예외

flag가 현재 인스턴스와는 다른 형식입니다.

예제

다음 예제에서는 고객이 레스토랑에서 주문할 수 있는 항목의 범주를 반영하는 열거형을 정의 DinnerItems 합니다. 이 예제에서는 고객이 앙트레와 음료를 주문했는지 여부를 테스트합니다.

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
open System

[<Flags>] 
type DinnerItems =
    | None = 0
    | Entree = 1
    | Appetizer = 2
    | Side = 4
    | Dessert = 8
    | Beverage = 16
    | BarBeverage = 32

let myOrder = 
    DinnerItems.Appetizer ||| DinnerItems.Entree ||| DinnerItems.Beverage ||| DinnerItems.Dessert
let flagValue = 
    DinnerItems.Entree ||| DinnerItems.Beverage
printfn $"{myOrder} includes {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

설명

이 메서드는 HasFlag 다음 부울 식의 결과를 반환합니다.

thisInstance And flag = flag

기본 값 flag 이 0이면 메서드가 반환됩니다 true. 이 동작이 바람직하지 않은 경우 다음 예제와 같이 메서드를 사용하여 Equals 0과 같은지 테스트하고 기본 값 flag 이 0이 아닌 경우에만 호출 HasFlag 할 수 있습니다.

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.
open System

[<Flags>] 
type Pets =
    | None = 0
    | Dog = 1
    | Cat = 2
    | Bird = 4
    | Rabbit = 8
    | Other = 16

let petsInFamilies = [| Pets.None; Pets.Dog ||| Pets.Cat; Pets.Dog |]
let mutable familiesWithoutPets = 0
let mutable familiesWithDog = 0

for petsInFamily in petsInFamilies do
    // Count families that have no pets.
    if petsInFamily.Equals Pets.None then
        familiesWithoutPets <- familiesWithoutPets + 1
    // Of families with pets, count families that have a dog.
    elif petsInFamily.HasFlag Pets.Dog then
        familiesWithDog <- familiesWithDog + 1

printfn $"{familiesWithoutPets} of {petsInFamilies.Length} families in the sample have no pets."
printfn $"{familiesWithDog} of {petsInFamilies} families in the sample have a dog."
// 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.

HasFlag 메서드는 특성으로 표시된 열거형 형식과 함께 FlagsAttribute 사용하도록 설계되었으며 여러 비트 필드가 설정되었는지 여부를 확인하는 데 사용할 수 있습니다. 특성으로 표시되지 않은 열거형 형식의 FlagsAttribute 경우 메서드 또는 메서드를 Equals CompareTo 호출합니다.

적용 대상

추가 정보