2. 2§. Indeksatorning turi va uning parametrlari turlari haqida


Indeksatorlarning sinf yoki strukturada ishlatilishi


Download 0.67 Mb.
bet4/18
Sana03.02.2023
Hajmi0.67 Mb.
#1151810
1   2   3   4   5   6   7   8   9   ...   18
Bog'liq
19.08 A Tojiyeva Saboxon Dasturlash kurs ishi

Indeksatorlarning sinf yoki strukturada ishlatilishi
Indeksatorlar sinf yoki strukturaning misollarini massivlar singari indeksatsiyalashga imkon beradi. Indekslangan qiymat elementning turini yoki nusxasini aniq ko'rsatmasdan o'rnatilishi yoki olinishi mumkin. Indeksatorlar xususiyatlar kabi ishlaydi , faqat ularning kirish parametrlari parametrlarni oladi.
Quyidagi misol qiymatlarni belgilash va olish uchun oddiy get va set accessors bilan umumiy sinfni belgilaydi . Sinf satrlarni saqlash uchun ushbu sinfning nusxasini yaratadi. Program
C # da ko‘rinishi
using System;


class SampleCollection
{
// Declare an array to store the data elements.
private T[] arr = new T[100];


// Define the indexer to allow client code to use [] notation.
public T this[int i]
{
get { return arr[i]; }
set { arr[i] = value; }
}
}


class Program
{
static void Main()
{
var stringCollection = new SampleCollection();
stringCollection[0] = "Hello, World";
Console.WriteLine(stringCollection[0]);
}
}
// The example displays the following output:
// Hello, World.
Ko'pincha, indeksator get yoki set accessorini shunchaki qaytaradigan yoki o'rnatadigan bitta gapdan iborat. Ifodani o'zida mujassam etgan a'zolar ushbu foydalanish holatini soddalashtirilgan sintaksis bilan ta'minlaydi. C # 6 dan boshlab faqat o'qish uchun indeksatorlar quyidagi misolda ko'rsatilgandek, ifoda a'zosi sifatida amalga oshirilishi mumkin.
C # da ko‘rinishi
using System;


class SampleCollection
{
// Declare an array to store the data elements.
private T[] arr = new T[100];
int nextIndex = 0;


// Define the indexer to allow client code to use [] notation.
public T this[int i] => arr[i];
public void Add(T value)
{
if (nextIndex >= arr.Length)
throw new IndexOutOfRangeException($"The collection can hold only {arr.Length} elements.");
arr[nextIndex++] = value;
}
}
}
}
class Program
{
static void Main()
{
var stringCollection = new SampleCollection();
stringCollection.Add("Hello, World");
System.Console.WriteLine(stringCollection[0]);
}
}
// The example displays the following output:
// Hello, World.
E'tibor bering, bu =>ibora tanasini ifodalaydi va kalit so'z getishlatilmaydi.
C # 7.0-dan boshlab, get va set accessors-ni ekspression tanasi bilan a'zolar sifatida amalga oshirish mumkin. Bunday holda siz ikkala kalit so'zni ( va ) belgilashingiz kerak . Misol: getset
C # da ko‘rinishi
using System;
class SampleCollection
{
// Declare an array to store the data elements.
private T[] arr = new T[100];
// Define the indexer to allow client code to use [] notation.
public T this[int i]
{
get => arr[i];
set => arr[i] = value;
}
}
class Program
{
static void Main()
{
var stringCollection = new SampleCollection();
stringCollection[0] = "Hello, World.";
Console.WriteLine(stringCollection[0]);
}
}
// The example displays the following output:
// Hello, World.

Download 0.67 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   18




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