SortedList ক্লাস (namespace: System.Collections) এর GetEnumerator() মেথড একটি IDictionaryEnumerator রিটার্ন করে যা কী-ভ্যালু জোড়াগুলোর মাধ্যমে ক্রমবদ্ধভাবে (কী অনুযায়ী সর্টেড) পুনরাবৃত্তি করতে দেয়।
উদাহরণ ১: SortedList এবং দুটি পদ্ধতিতে পুনরাবৃত্তি
using System;
using System.Collections;
public class Demo {
public static void Main(string[] args) {
SortedList sortedList = new SortedList();
sortedList.Add("A", "1");
sortedList.Add("B", "2");
sortedList.Add("C", "3");
sortedList.Add("D", "4");
sortedList.Add("E", "5");
sortedList.Add("F", "6");
sortedList.Add("G", "7");
sortedList.Add("H", "8");
sortedList.Add("I", "9");
sortedList.Add("J", "10");
Console.WriteLine("SortedList উপাদানসমূহ (foreach)...");
foreach (DictionaryEntry d in sortedList) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("SortedList কী-ভ্যালু জোড়া সংখ্যা = " + sortedList.Count); // ১০
Console.WriteLine("\nEnumerator দিয়ে পুনরাবৃত্তি (GetEnumerator)...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
আউটপুট
SortedList উপাদানসমূহ (foreach)... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10 SortedList কী-ভ্যালু জোড়া সংখ্যা = 10 Enumerator দিয়ে পুনরাবৃত্তি (GetEnumerator)... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10
উদাহরণ ২: স্ট্রিং কীযুক্ত SortedList এবং এনুমারেটর (অ্যালফাবেটিক্যালি সর্টেড)
using System;
using System.Collections;
public class Demo {
public static void Main(string[] args) {
SortedList sortedList = new SortedList();
sortedList.Add("One", "1");
sortedList.Add("Two", "2");
sortedList.Add("Three", "3");
sortedList.Add("Four", "4");
sortedList.Add("Five", "5");
sortedList.Add("Six", "6");
sortedList.Add("Seven", "7");
sortedList.Add("Eight", "8");
sortedList.Add("Nine", "9");
sortedList.Add("Ten", "10");
Console.WriteLine("Enumerator দিয়ে পুনরাবৃত্তি (সর্টেড অর্ডার)...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
আউটপুট
Enumerator দিয়ে পুনরাবৃত্তি (সর্টেড অর্ডার)... Key = Eight, Value = 8 Key = Five, Value = 5 Key = Four, Value = 4 Key = Nine, Value = 9 Key = One, Value = 1 Key = Seven, Value = 7 Key = Six, Value = 6 Key = Ten, Value = 10 Key = Three, Value = 3 Key = Two, Value = 2
SortedList.GetEnumerator() - গুরুত্বপূর্ণ বিষয়সমূহ
- রিটার্ন টাইপ:
IDictionaryEnumerator(নন-জেনেরিক) - প্রোপার্টি:
Key,Value,Entry(DictionaryEntry) - মেথড:
MoveNext(),Reset() - ক্রম: কী অনুযায়ী অ্যাসেন্ডিং অর্ডার (স্ট্রিং কী-তে লেক্সিকোগ্রাফিক্যালি)
- স্ন্যাপশট নয়: এনুমারেশন চলাকালীন কালেকশন মডিফাই করলে
InvalidOperationExceptionনিক্ষেপ করে।
আধুনিক পদ্ধতি: foreach (সুপারিশকৃত)
// পদ্ধতি ১: DictionaryEntry (নন-জেনেরিক)
foreach (DictionaryEntry entry in sortedList) {
Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
}
// পদ্ধতি ২: var (ইমপ্লিসিট টাইপিং)
foreach (var entry in sortedList) {
Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
}
// পদ্ধতি ৩: Keys/Values আলাদা
foreach (var key in sortedList.Keys) {
Console.WriteLine($"Key: {key}, Value: {sortedList[key]}");
}
জেনেরিক সংস্করণ: SortedList<TKey, TValue> (সুপারিশকৃত)
using System.Collections.Generic;
var sortedList = new SortedList<string, int>();
sortedList.Add("A", 1);
sortedList.Add("B", 2);
// ...
// জেনেরিক এনুমারেটর (IEnumerator>)
foreach (var kvp in sortedList) {
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
// IDictionaryEnumerator নয়, বরং IEnumerator>
// টাইপ-সেফ, বক্সিং/আনবক্সিং নেই
SortedList vs SortedDictionary - এনুমারেশন পারফরম্যান্স
| দিক | SortedList | SortedDictionary |
|---|---|---|
| এনুমারেশন স্পিড | ফাস্ট (অ্যারে-বেসড, ক্যাশে-ফ্রেন্ডলি) | স্লোওয়ার (ট্রি ট্রাভার্সাল) |
| ইনসারশন/ডিলিশন | O(n) | O(log n) |
| মেমরি | কম | উচ্চ |
| ইন্ডেক্স অ্যাক্সেস | হ্যাঁ O(1) | না |
সুপারিশ: নতুন কোডে SortedList<TKey, TValue> (জেনেরিক) ব্যবহার করুন। নন-জেনেরিক SortedList লিগ্যাসি কোডের জন্য।