Enum.HasFlag(Enum) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan apakah satu atau beberapa bidang bit diatur dalam instans saat ini.
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
Nilai enumerasi.
Mengembalikan
true
jika bidang bit atau bidang bit yang diatur flag
juga diatur dalam instans saat ini; jika tidak, false
.
Pengecualian
flag
adalah jenis yang berbeda dari instans saat ini.
Contoh
Contoh berikut mendefinisikan DinnerItems
enumerasi yang mencerminkan kategori item yang dapat dipesan pelanggan di restoran. Contohnya menguji apakah pelanggan telah memesan minuman dan minuman.
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
Keterangan
Metode mengembalikan HasFlag hasil ekspresi Boolean berikut.
(thisInstance & flag) == flag
Jika nilai yang mendasar true
adalah flag
nol, metode akan mengembalikan . Jika perilaku ini tidak diinginkan, Anda dapat menggunakan Equals metode untuk menguji kesetaraan dengan nol dan memanggil HasFlag hanya jika nilai yang mendasar flag
bukan nol, seperti yang diilustrasikan contoh berikut.
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.
Metode HasFlag ini dirancang untuk digunakan dengan jenis enumerasi yang ditandai dengan FlagsAttribute atribut dan dapat digunakan untuk menentukan apakah beberapa bidang bit diatur. Untuk jenis enumerasi yang tidak ditandai dengan FlagsAttribute atribut , panggil Equals metode atau CompareTo metode .