OrderedDictionary ক্লাস (System.Collections.Specialized) একটি কী/মান কালেকশন যা সন্নিবেশ ক্রম (insertion order) রক্ষা করে এবং কী বা সূচক (index) দুইভাবে অ্যাক্সেস সমর্থন করে। এটি IDictionary, ICollection, IEnumerable বাস্তবায়ন করে। কী অবশ্যই ইউনিক। আধুনিক বিকল্প: Dictionary<TKey, TValue> (ক্রম নিশ্চিত নয়, .NET 6+ এ সন্নিবেশ ক্রম রক্ষা করে) বা OrderedDictionary<TKey, TValue> (তৃতীয় পক্ষের লাইব্রেরি)।
উদাহরণ ১: OrderedDictionary তৈরি, এনামারেশন এবং Remove
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
dict.Add("C", "Smart Wearables");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("OrderedDictionary উপাদানসমূহ...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("OrderedDictionary-এ উপাদান সংখ্যা = " + dict.Count);
dict.Remove("E");
Console.WriteLine("OrderedDictionary-এ উপাদান সংখ্যা (আপডেটেড) = " + dict.Count);
}
}
আউটপুট
OrderedDictionary উপাদানসমূহ... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear OrderedDictionary-এ উপাদান সংখ্যা = 6 OrderedDictionary-এ উপাদান সংখ্যা (আপডেটেড) = 5
উদাহরণ ২: Keys কালেকশন কপি করা
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
dict.Add("6", "Six");
dict.Add("7", "Seven");
dict.Add("8", "Eight");
ICollection col = dict.Keys;
String[] strKeys = new String[dict.Count];
col.CopyTo(strKeys, 0);
Console.WriteLine("কীগুলো প্রদর্শন করা হচ্ছে...");
for (int i = 0; i < dict.Count; i++) {
Console.WriteLine(strKeys[i]);
}
}
}
আউটপুট
কীগুলো প্রদর্শন করা হচ্ছে... 1 2 3 4 5 6 7 8
দ্রষ্টব্য: OrderedDictionary নন-জেনেরিক। কী এবং মান object টাইপের। টাইপ সেফティ এবং পারফরম্যান্সের জন্য জেনেরিক বিকল্প ব্যবহার করুন। Remove(key) দিয়ে কী দিয়ে মুছে ফেলা যায়।