Ranglar va fon
Sanab o'tiluvchi tur Season a = Season.Autumn
Download 76.4 Kb.
|
13. Kortej, enum
Sanab o'tiluvchi turSeason a = Season.Autumn;Console.WriteLine($"{a} ning sonli qiymati - {(int)a}");// Autumn ning sonli qiymati - 2var b = (Season)1;Console.WriteLine(b); // Summervar c = (Season)4;Console.WriteLine(c); // 4string fasl = "Qish";if (Enum.IsDefined(typeof(Season), fasl)) {Season s = (Season)Enum.Parse(typeof(Season), fasl);Console.Write($"Season enumda {s} aniqlangan");Console.WriteLine($" va uning qiymati {(int)s}");}else {Console.WriteLine($"Season enumda '{fasl}' aniqlanmagan");}Sanab o'tiluvchi tur bitlar sifatidaSanab o'tiluvchi tur bir qancha tanlovlar kombinatsiyasi sifatida ishlatish uchun har bir elementni alohida bit maydoni sifatida ishlatish zarur. Ya'ni, elementlar qiymatlari 2 ning darajalari bo'lishi kerak. Sanab o'tiluvchi tur shunday ishlashi uchun Flags atributini qo'shish kerak.[Flags]public enum Days{Monday = 0b_0000_0001, // 1Tuesday = 0b_0000_0010, // 2Wednesday = 0b_0000_0100, // 4Thursday = 0b_0000_1000, // 8Friday = 0b_0001_0000, // 16Saturday = 0b_0010_0000, // 32Sunday = 0b_0100_0000, // 64Weekend = Saturday | Sunday}Sanab o'tiluvchi tur bitlar sifatidaDays meetingDays = Days.Monday | Days.Wednesday | Days.Friday;Console.WriteLine(meetingDays);// Monday, Wednesday, FridayDays workingFromHomeDays = Days.Thursday | Days.Friday;Console.WriteLine($"Join a meeting by phone on {meetingDays & workingFromHomeDays}");// Join a meeting by phone on Fridaybool isMeetingOnTuesday = (meetingDays & Days.Tuesday) == Days.Tuesday;Console.WriteLine($"Is there a meeting on Tuesday: {isMeetingOnTuesday}");// Is there a meeting on Tuesday: Falsevar a = (Days)37;Console.WriteLine(a);// Monday, Wednesday, SaturdaySavollar?Download 76.4 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling