C#-এ ListDictionary (System.Collections.Specialized) একটি সাধারণ ডিকশনারি ইমপ্লিমেন্টেশন যা ছোট কালেকশনের জন্য অপ্টিমাইজড। এটি সিঙ্গলি লিঙ্কড লিস্ট ব্যবহার করে ডেটা সংরক্ষণ করে। বড় ডেটাসেটের জন্য HybridDictionary বা Dictionary<TKey, TValue> ভালো।
উদাহরণ ১: ListDictionary তৈরি ও প্রপার্টি চেক
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add("1", "SUV");
dict.Add("2", "Sedan");
dict.Add("3", "Utility Vehicle");
dict.Add("4", "Compact Car");
dict.Add("5", "SUV");
dict.Add("6", "Sedan");
dict.Add("7", "Utility Vehicle");
dict.Add("8", "Compact Car");
dict.Add("9", "Crossover");
dict.Add("10", "Electric Car");
Console.WriteLine("ListDictionary এলিমেন্টসমূহ...");
foreach (DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nListDictionary ফিক্সড সাইজ? = " + dict.IsFixedSize);
Console.WriteLine("ListDictionary রিড-অনলি? = " + dict.IsReadOnly);
Console.WriteLine("ListDictionary সিঙ্ক্রোনাইজড? = " + dict.IsSynchronized);
Console.WriteLine("কী 'K' আছে কি? = " + dict.Contains("K"));
Console.WriteLine("কী '9' আছে কি? = " + dict.Contains("9"));
}
}
আউটপুট
ListDictionary এলিমেন্টসমূহ... 1 SUV 2 Sedan 3 Utility Vehicle 4 Compact Car 5 SUV 6 Sedan 7 Utility Vehicle 8 Compact Car 9 Crossover 10 Electric Car ListDictionary ফিক্সড সাইজ? = False ListDictionary রিড-অনলি? = False ListDictionary সিঙ্ক্রোনাইজড? = False কী 'K' আছে কি? = False কী '9' আছে কি? = True
উদাহরণ ২: একাধিক ListDictionary ও ক্লোনিং
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 এলিমেন্টসমূহ...");
foreach (DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nListDictionary2 এলিমেন্টসমূহ...");
foreach (DictionaryEntry d in dict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Dictionary 2-এ কী/ভ্যালু জোড়া সংখ্যা = " + dict2.Count);
ListDictionary dict3 = new ListDictionary();
dict3 = dict2; // রেফারেন্স কপি
Console.WriteLine("\nListDictionary3 কি ListDictionary2-এর সমান? = " + (dict3.Equals(dict2)));
dict3.Clear();
Console.WriteLine("Dictionary 3-এ কী/ভ্যালু জোড়া সংখ্যা = " + dict3.Count);
}
}
আউটপুট
ListDictionary1 এলিমেন্টসমূহ... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear ListDictionary2 এলিমেন্টসমূহ... 1 One 2 Two 3 Three 4 Four 5 Five Six Dictionary 2-এ কী/ভ্যালু জোড়া সংখ্যা = 6 ListDictionary3 কি ListDictionary2-এর সমান? = True Dictionary 3-এ কী/ভ্যালু জোড়া সংখ্যা = 0
ব্যাখ্যা: ListDictionary ছোট ডেটাসেট (সাধারণত < ১০ আইটেম) এর জন্য দ্রুত। dict3 = dict2 শুধু রেফারেন্স কপি করে, তাই dict3.Clear() কল করলে dict2-ও খালি হয়ে যায়। স্বতন্ত্র কপির জন্য ম্যানুয়াল কপি করতে হয়।