SortedList ক্লাস (namespace: System.Collections) একটি সর্টেড কী-ভ্যালু সংগ্রহ যা কী অনুযায়ী স্বয়ংক্রিয় সর্টেড থাকে। এর Clear() মেথড সব কী-ভ্যালু জোড়া একসাথে সরিয়ে দেয় এবং Count শূন্য করে দেয়।
উদাহরণ ১: বড় SortedList Clear()
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 (DictionaryEntry d in sortedList) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("SortedList কী-ভ্যালু জোড়া সংখ্যা = " + sortedList.Count); // ১০
// সব উপাদান সরানো
sortedList.Clear();
Console.WriteLine("SortedList-র কী-ভ্যালু জোড়া সংখ্যা (আপডেট) = " + sortedList.Count); // ০
}
}
আউটপুট
SortedList উপাদানসমূহ... 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 SortedList-র কী-ভ্যালু জোড়া সংখ্যা (আপডেট) = 0
উদাহরণ ২: ছোট SortedList Clear()
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");
Console.WriteLine("SortedList কী-ভ্যালু জোড়া সংখ্যা = " + sortedList.Count); // ৩
Console.WriteLine("SortedList উপাদানসমূহ...");
foreach (DictionaryEntry d in sortedList) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
sortedList.Clear();
Console.WriteLine("SortedList কী-ভ্যালু জোড়া সংখ্যা (আপডেট) = " + sortedList.Count); // ০
}
}
আউটপুট
SortedList কী-ভ্যালু জোড়া সংখ্যা = 3 SortedList উপাদানসমূহ... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 SortedList কী-ভ্যালু জোড়া সংখ্যা (আপডেট) = 0
SortedList-এর মূল বৈশিষ্ট্য
- সর্টেড: কী অনুযায়ী স্বয়ংক্রিয় সর্টেড (IComparer ব্যবহার করে)।
- ইন্ডেক্স অ্যাক্সেস:
GetKey(index),GetByIndex(index),[index]সাপোর্ট করে। - দুই অ্যারে: আন্ডারলাইংলে দুটি অ্যারে (keys[], values[]) রক্ষা করে।
- নন-জেনেরিক:
objectকী এবং ভ্যালু (বক্সিং/আনবক্সিং ওভারহেড)। - পারফরম্যান্স: Insert/Remove O(n) - অ্যারে শিফটিং দরকার।
SortedList vs SortedDictionary vs Dictionary
| বৈশিষ্ট্য | SortedList | SortedDictionary | Dictionary |
|---|---|---|---|
| নেমস্পেস | System.Collections | System.Collections.Generic | System.Collections.Generic |
| টাইপ সেফটি | নন-জেনেরিক | জেনেরিক | জেনেরিক |
| সর্টেড | হ্যাঁ | হ্যাঁ | না |
| ইন্ডেক্স অ্যাক্সেস | হ্যাঁ O(1) | না | না |
| ইনসারশন/ডিলিশন | O(n) | O(log n) | O(1) গড়ে |
| মেমরি | কম (অ্যারে) | উচ্চ (ট্রি নোডস) | মাঝারি |
আধুনিক জেনেরিক সংস্করণ: SortedList<TKey, TValue>
using System.Collections.Generic;
var sortedList = new SortedList<string, int>();
sortedList.Add("A", 1);
sortedList.Add("B", 2);
// ...
sortedList.Clear(); // একই আচরণ, টাইপ-সেফ
সুপারিশ: নতুন কোডে SortedList<TKey, TValue> (জেনেরিক) ব্যবহার করুন। নন-জেনেরিক SortedList লিগ্যাসি কোডের জন্য।