Interfeys toifalari tushunchasi Interfeys toifalarni abstract bazaviy sinflar bilan taqqoslash Maxsus interfeyslarni kiritish


Download 0.77 Mb.
bet39/50
Sana26.01.2023
Hajmi0.77 Mb.
#1129439
1   ...   35   36   37   38   39   40   41   42   ...   50
Bog'liq
MAVZULAR UMUMIY

To’plam. Tartiblangan to’plam
To’plamlar bilan ishlash uchun birato’la tartiblangan to’plamni amalga oshiruvchi (realizatsiya qiluvchi) SortedSet sinfidan foydalaniladi. Biroq bu sinfga saralashni (tartiblashni) qanday amalga oshirish ko’rsatilishi lozim, bu ishni IComparer interfeysi orqali qilish mumkin. Bu interfeyni masalan, quyidagicha yozish mumkin:
class SortPeopleByAge : IComparer

{
public int Compare(Person firstPerson, Person secondPerson)


{
if (firstPerson?.Age > secondPerson?.Age)
{
return 1;
}
if (firstPerson?.Age < secondPerson?.Age)
{
return -1;
}
return 0;
}
}
Shundan so’ng, to’plamlar bilan ishlashni namoyish etish uchun Main() metodi ushbu metodga murojaat qilishi mumkin:
static void UseSortedSet()
{
// Person ob’yektlarini turli yoshdagilar bilan yaratish
SortedSet
setOfPeople =
new SortedSet
(new SortPeopleByAge())
{
new Person {FirstName= "Homer", LastName="Simpson", Age=47},
new Person {FirstName= "Marge", LastName="Simpson", Age=45},
new Person {FirstName= "Lisa", LastName="Simpson", Age=9},
new Person {FirstName= "Bart", LastName="Simpson", Age=8}
};
// Elementlar yosh bo’yicha tartiblanganiga ahamiyat bering
foreach (Person р in setOfPeople)
{
Console.WriteLine(р);
}
Console.WriteLine() ;
// Yana bir nechta Person ob’yekti qo’shish, turli yoshdagi
setOfPeople.Add(new Person { FirstName = "Saku",
LastName = "Jones", Age = 1 });
setOfPeople.Add(new Person { FirstName = "Mikko",
LastName = "Jones", Age = 32 });
// Elementlar avvalgidek tartiblangan holatda bo’ladi
foreach (Person р in setOfPeople)
{
Console.WriteLine(p);
}
}
Bu metodni ishlash natijasi:
Name: Bart Simpson, Age: 8
Name: Lisa Simpson, Age: 9
Name: Marge Simpson, Age: 45
Name: Homer Simpson, Age: 47

Name: Saku Jones, Age: 1


Name: Bart Simpson, Age: 8
Name: Lisa Simpson, Age: 9
Name: Mikko Jones, Age: 32
Name: Marge Simpson, Age: 45
Name: Homer Simpson, Age: 4

Lug’atlar


Yana bi umumlashgan kollektsiya bu – lug’atlardir, ular Dictionary sinfi orqali amalga oshiriladi. Bunda birinchi parameter (TKey) unikal bo’lishi lozim, aks holda bajarilish jarayonidagi xatolik kelib chiqadi.
private static void UseDictionary()
{
// Add() metodi bilan to’ldirish
Dictionary peopleA = new Dictionary();
peopleA.Add ( "Homer" , new Person { FirstName = ’’Homer",
LastName = "Simpson", Age = 47 });
peopleA.Add("Marge", new Person { FirstName = "Marge",
LastName = "Simpson", Age = 45 });
peopleA.Add("Lisa", new Person { FirstName = "Lisa",
LastName = "Simpson", Age = 9 });
// Homer kalitli elementni olish
Person homer = peopleA["Homer"];
Console.WriteLine(homer);
// Initsializatsiya sintaksisi bilan to’ldirish
Dictionary peopleB = new Dictionary()
{
{"Homer", new Person { FirstName = "Homer",
LastName = "Simpson", Age = 47}},
{"Marge", newPerson { FirstName = "Marge",
LastName = "Simpson", Age = 45 } },
{ "Lisa", new Person { FirstName = "Lisa",
LastName = "Simpson", Age = 9 } }
};
// Lisa kalitli elementni olish
Person lisa = peopleB["Lisa"];
Console.WriteLine(lisa);
}
Lug’atni to’ldirishning yana boshqa bir usuli:
// Lug’atni initsializatsiya qilish bilan to’ldirish
Dictionary peopleC = new Dictionary()
{
["Homer"] = new Person { FirstName = "Homer",
LastName = "Simpson", Age = 47 },
["Marge"] = new Person { FirstName = "Marge",
LastName = "Simpson", Age = 45 },
["Lisa"] = new Person { FirstName = "Lisa",
LastName = "Simpson", Age = 9 }
};

Download 0.77 Mb.

Do'stlaringiz bilan baham:
1   ...   35   36   37   38   39   40   41   42   ...   50




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