Tuple

Tuple

Keling bu mavzuni sodda qilib o’rganish uchun o’xshatish metodidan foydalanamiz 🙂 O’zimiz bilgan, tushungan massiv ga o’xshatib ko’ramiz shunda soddaroq bo’ladi. Qani ketdik.

Tuple classi ni massivga o’xshagan to’plam deb qarasak boladi. Undan farqi har-xil turdagi ma’lumotlarni saqlash mumkin. Keling uni sintaksisi bilan tanishamiz:

Tupleint, string, string> person = new Tuple int, string, string>(1, "Steve", "Jobs");

//yoki 

var person = Tuple.Create(1, "Steve", "Jobs");

Tuple da ko’pi bilan 8 ta element saqlash mumkin. Agar undan ko’payib ketsa, kompilyator bizga xatolik beradi. Lekin bunda ham yechim bor har doimgidek ayyorlik ishlatamiz 🙂 Tuple ichida tuple yozamiz quyidagicha:

var numbers = Tuple.Create(1, 2, Tuple.Create(3, 4, 5, 6, 7,  8), 9, 10, 11, 12, 13 );

Tuplega murojat class maydonlariga murojaat qilishga o’xshab ketadi.

a]:text-teal-600 [&>a:hover]:text-teal-800 [&>code]:bg-teal-600/4 [&>code]:text-inherit [&>code]:shadow-none text-teal-800 fill-teal-700 stroke-teal-700 dark:text-teal-100 dark:[&>a]:text-teal-400 dark:[&>a:hover]:text-teal-500 dark:[&>code]:bg-teal-200/2 dark:[&>code]:text-inherit decoration-teal/6 dark:decoration-teal/6 dark:fill-teal-400 dark:stroke-teal-300 flip-heading-hash»>obyektNomi.Item1, obyektNomi.Item2 … obyektNomi.Item8
var person = Tuple.Create(1, "Steve", "Jobs"); 

person.Item1; // returns 1 

person.Item2; // returns "Steve" 

person.Item3; // returns "Jobs"

8 — elementga person.Rest ko’rinishda murojat qilsa ham bo’ladi. person.Item8 dan farqi Rest qiymatni qovus ga olib chiqaradi.

a]:text-periwinkle-700 [&>a:hover]:text-periwinkle-800 [&>code]:bg-periwinkle-700/4 [&>code]:text-inherit [&>code]:shadow-none text-periwinkle-900 fill-periwinkle-700 stroke-periwinkle-700 dark:text-periwinkle-200 dark:[&>a]:text-periwinkle dark:[&>a:hover]:text-periwinkle-600 dark:[&>code]:bg-periwinkle-200/2 dark:[&>code]:text-inherit decoration-periwinkle-700/6 dark:decoration-periwinkle/6 dark:fill-periwinkle-400 dark:stroke-periwinkle-400 flip-heading-hash»> E’tibor bering faqat 8- elementga oxirgi elementga EMAS
var numbers = Tuple.Create("One", 2, 3, "Four", 5, "Six", 7, 8);
Console.WriteLine(numbers.Item1); // print "One"
Console.WriteLine(numbers.Item2); // print 2
Console.WriteLine(numbers.Item3); // print 3
Console.WriteLine(numbers.Item4); // print "Four"
Console.WriteLine(numbers.Item5); // print 5
Console.WriteLine(numbers.Item6); // print "Six"
Console.WriteLine(numbers.Item7); // print 7
Console.WriteLine(numbers.Rest);  // print (8)
Console.WriteLine(numbers.Rest.Item1); // print 8

Tupledan, funksiyalarda argument sifatida foydalanish ham mumkin.

static void Main(string[] args)
{
    var person = Tuple.Create(1, "Steve", "Jobs");
    DisplayTuple(person);
}

static void DisplayTuple(Tupleint,string,string> person)
{
    Console.WriteLine($"Id = { person.Item1}");
    Console.WriteLine($"First Name = { person.Item2}");
    Console.WriteLine($"Last Name = { person.Item3}");
} 

Funksiylardan Tuple toifasiga mansub qiymatlarni ham qabul qilib olishimiz mumkin:

static void Main(string[] args)
{
    var person = GetPerson();
}

static Tupleint, string, string> GetPerson() 
{
    return Tuple.Create(1, "Bill", "Gates");
}

Tupledan foydalanishning afzallik va kamchiliklari

Qulayliklari:

    Funksiyalardan bir necha qiymatlarnii qabul qilish / yuborish

  • Key/Value ko’rinishida ishlash imkoniyati

Kamchiliklari:

    string toifasi kabi Tuple immutable tur hisoblanadi. Yaratilgandan keyin uning qiymatlariga o’zgartirish kiritib bo’lmaydi.

  • Tuple qiymatli emas balki ma’lumotli (reference) toifa hisoblanadi. Buning hisobiga, u CPU ya’ni protsessorni zo’riqishga olib keladi.

Dasturlash
Tuple