Forem

Farkhodbek Kamolov
Farkhodbek Kamolov

Posted on

Nol indekslangan to'plamlar

C# da bir nechta to'plam(collection type) turlari nol indekslangan, ya'ni ularning qiymatiga indeks 0 dan boshlab murojat qilish mumkin. Yani arrayni yoki listni 0-chi indeksga murojat qilsak, bizga 1-chi element(qiymati)ni qaytaradi.

Misol:

var nums = new int[]{1,2,3,4,5};
Console.WriteLine(nums[0]);
// output --> 1
Enter fullscreen mode Exit fullscreen mode

C# da nol indeksli to'plamlar ro'yxatidan bir shingil: array, List<T>, ArrayList va hokazolar kiradi.

Yuqorida ko'rganimizdek, bizda nums nomi bilan, 1 dan 5 gacha bolgan sonlar arrayi berilgan, va biz konsolga birinchi elementi(qiymatini)ni chiqarishimiz uchun nums[0] deb murojat qildik. Keyingi elementiga murojat qilish uchun esa nums[1] deyish kifoya, va shu zaynda davom etaveradi.

0 indeksli toplamlarni ohirgi elementiga(umumiy elementlar soni n ga teng bolsa) nums[n-1] qilib murojat qilinadi. Nima uchun aynan n-1?!
Buning sababi, yuqoridagi kodga yuzlansak, u yerda qiymatlarning umumiy soni 5 ga teng(n = 5), indeks bo'ylab sanalganda esa 4 ga teng (0,1,2,3,4). Agar nums[5] qilsak, indeks bo'yicha sanalganda 5-chi indeks 6-chi elementga to'g'ri keladi, bizda esa umumiy 5ta qiymatlar bor.

indekslar -> 0 | 1 | 2 | 3 | 4
qiymatlar-> 1 | 2 | 3 | 4 | 5

Misol:

var nums = new int[]{1,2,3,4,5};
int n = nums.Length; // n = 5,
Console.WriteLine(nums[n-1]);
// output --> 5
Enter fullscreen mode Exit fullscreen mode

Bu kodda nums.Length(length -> uzunligi) bizga n yaniy qiymatlar sonini topishda yordam beradi. Yuqoridagi kodimizni yanda qisqartirsak boladi.

Misol:

var nums = new int[]{1,2,3,4,5};
Console.WriteLine(nums[nums.Length - 1]);
// output --> 5
Enter fullscreen mode Exit fullscreen mode

Bazida shunday holatlar boladiki ohirgi elementga murojat qilyotkanimizda nums[nums.Length] -1 ni qo'shib ketish yoddan chiqishi mumkin, bu esa o'z vaqtida hatolikga olib keladi.
C# 13-chi versiyasidan boshlab yangi feature(xususiyat) kirib keldi uning nomini
Implicit index access -> Indeksga yashirin kirish deb nomlashdi, va bu xususiyatni ishlatish uchun esa ^(caret -> karet) aperatori bilan amalga oshirish mumkin. Bu feature xaqida ushbu -> link postimda tanishib chiqsangiz boladi.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay