Interfeys toifalari tushunchasi Interfeys toifalarni abstract bazaviy sinflar bilan taqqoslash Maxsus interfeyslarni kiritish
Download 0.77 Mb.
|
MAVZULAR UMUMIY
To’plam. Tartiblangan to’plam
To’plamlar bilan ishlash uchun birato’la tartiblangan to’plamni amalga oshiruvchi (realizatsiya qiluvchi) SortedSet class SortPeopleByAge : IComparer {
{ 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 private static void UseDictionary() { // Add() metodi bilan to’ldirish 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 { {"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 { ["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: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling