Ranglar va fon


Sanab o'tiluvchi tur Season a = Season.Autumn


Download 76.4 Kb.
bet4/4
Sana10.10.2023
Hajmi76.4 Kb.
#1696964
1   2   3   4
Bog'liq
13. Kortej, enum

Sanab o'tiluvchi tur

Season a = Season.Autumn;

Console.WriteLine($"{a} ning sonli qiymati - {(int)a}");

// Autumn ning sonli qiymati - 2

var b = (Season)1;

Console.WriteLine(b); // Summer

var c = (Season)4;

Console.WriteLine(c); // 4

string 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 sifatida

Sanab 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, // 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

}

Sanab o'tiluvchi tur bitlar sifatida

Days meetingDays = Days.Monday | Days.Wednesday | Days.Friday;

Console.WriteLine(meetingDays);

// Monday, Wednesday, Friday

Days workingFromHomeDays = Days.Thursday | Days.Friday;

Console.WriteLine($"Join a meeting by phone on {meetingDays & workingFromHomeDays}");

// Join a meeting by phone on Friday

bool isMeetingOnTuesday = (meetingDays & Days.Tuesday) == Days.Tuesday;

Console.WriteLine($"Is there a meeting on Tuesday: {isMeetingOnTuesday}");

// Is there a meeting on Tuesday: False

var a = (Days)37;

Console.WriteLine(a);

// Monday, Wednesday, Saturday

Savollar?


Download 76.4 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling