OrderedDictionary ক্লাস একটি কী/মান কালেকশন যা সন্নিবেশ ক্রম (insertion order) অনুযায়ী সাজানো থাকে এবং কী বা সূচক (index) দুয়েনি দ্বারা অ্যাক্সেস সমর্থন করে। এটি System.Collections.Specialized নেমস্পেসে অবস্থিত এবং IDictionary, ICollection, IEnumerable ইন্টারফেস বাস্তবায়ন করে।
প্রোপার্টিগুলো
| ক্রমিক নং | প্রোপার্টি ও বর্ণনা |
|---|---|
| ১ | Count কী/মান জোড়ার মোট সংখ্যা রিটার্ন করে। |
| ২ | IsReadOnly কলেকশন রিড-ওনলি কিনা নির্দেশ করে। |
| ৩ | Item[Int32] নির্দিষ্ট সূচকের মান পাওয়ার/সেট করার সুযোগ দেয়। |
| ৪ | Item[Object] নির্দিষ্ট কী-এর মান পাওয়ার/সেট করার সুযোগ দেয়। |
| ৫ | Keys সব কী ধারণকারী ICollection রিটার্ন করে। |
| ৬ | Values সব মান ধারণকারী ICollection রিটার্ন করে। |
মূল মেথডসমূহ
| ক্রমিক নং | মেথড ও বর্ণনা |
|---|---|
| ১ | Add(Object, Object) নির্দিষ্ট কী এবং মান দিয়ে এন্ট্রি যোগ করে (সর্বনিম্ন উপলব্ধ সূচকে)। |
| ২ | AsReadOnly() রিড-ওনলি কপি রিটার্ন করে। |
| ৩ | Clear() সব এলিমেন্ট মুছে দেয়। |
| ৪ | Contains(Object) নির্দিষ্ট কী আছে কিনা যাচাই করে। |
| ৫ | CopyTo(Array, Int32) এলিমেন্টগুলো একমাত্রি অ্যারেতে কপি করে। |
| ৬ | Equals(Object) অবজেক্ট সমতা যাচাই করে (ইনহেরিটেড)। |
| ৭ | GetEnumerator() IDictionaryEnumerator রিটার্ন করে যা কালেকশনের মাধ্যমে পুনরাবৃত্তি করে। |
উদাহরণ ১: Count এবং Clear() ব্যবহার
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Home Appliances");
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.Clear();
Console.WriteLine("OrderedDictionary-এ উপাদান সংখ্যা (আপডেটেড) = " + dict.Count);
}
}
আউটপুট
OrderedDictionary উপাদানসমূহ... A Home Appliances B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear OrderedDictionary-এ উপাদান সংখ্যা = 6 OrderedDictionary-এ উপাদান সংখ্যা (আপডেটেড) = 0
উদাহরণ ২: Clear() এর আরও একটি উদাহরণ
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.Clear();
Console.WriteLine("OrderedDictionary-এ উপাদান সংখ্যা (আপডেটেড) = " + dict.Count);
}
}
আউটপুট
OrderedDictionary উপাদানসমূহ... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear OrderedDictionary-এ উপাদান সংখ্যা = 6 OrderedDictionary-এ উপাদান সংখ্যা (আপডেটেড) = 0
দ্রষ্টব্য: OrderedDictionary লিগেসি নন-জেনেরিক ক্লাস। নতুন প্রজেক্টে Dictionary<TKey, TValue> (NET 6+ এ সন্নিবেশ ক্রম রক্ষা করে) বা ImmutableDictionary<TKey, TValue> ব্যবহার করা ভালো।