Enumeration typelar kombinatsiyasini ifodalashini xohlasek, ushbu tanlovlar uchun enum a'zolarini belgilashimiz kere. Ya'ni, bu enum a'zolarining bog'langan qiymatlari ikkining vakolatlari bo'lishi kerak. Keyin, siz bitli mantiqiy operatorlardan foydalanishingiz mumkin |
yoki &
mos ravishda tanlovlarni birlashtirish yoki tanlovlar birikmalarini kesish uchun. Sanoq turi bit maydonlarini e'lon qilishini ko'rsatish uchun unga Flags atributini qo'llang:
[Flags]
public enum Days
{
None = 0b_0000_0000, // 0
Monday = 0b_0000_0001, // 1
Tuesday = 0b_0000_0010, // 2
Wednesday = 0b_0000_0100, // 4
Thursday = 0b_0000_1000, // 8
Friday = 0b_0001_0000, // 16
Saturday = 0b_0010_0000, // 32
Sunday = 0b_0100_0000, // 64
Weekend = Saturday | Sunday
}
public class FlagsEnumExample
{
public static void Main()
{
Days meetingDays = Days.Monday | Days.Wednesday | Days.Friday;
Console.WriteLine(meetingDays);
// Output:
// Monday, Wednesday, Friday
Days workingFromHomeDays = Days.Thursday | Days.Friday;
Console.WriteLine($"Join a meeting by phone on {meetingDays & workingFromHomeDays}");
// Output:
// Join a meeting by phone on Friday
bool isMeetingOnTuesday = (meetingDays & Days.Tuesday) == Days.Tuesday;
Console.WriteLine($"Is there a meeting on Tuesday: {isMeetingOnTuesday}");
// Output:
// Is there a meeting on Tuesday: False
var a = (Days)37;
Console.WriteLine(a);
// Output:
// Monday, Wednesday, Saturday
}
}
yesli cho' Enumeration types as bit flags
Top comments (0)