C#-এ ListDictionary ক্লাসের Count প্রোপার্টি ব্যবহার করে ডিকশনারিতে কতগুলো কী-মান জোড়া আছে তা জানা যায়। 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);
// রেফারেন্স কপি এবং Clear() এর প্রভাব
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 6 Six Dictionary 2-র কী-মান জোড়ার সংখ্যা = 6 ListDictionary3 কি ListDictionary2-এর সমান? = True Dictionary 3-র কী-মান জোড়ার সংখ্যা = 0
উদাহরণ ২: IDictionaryEnumerator ব্যবহার করে ইটারেশন
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");
Console.WriteLine("ListDictionary1 উপাদান...");
foreach (DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Dictionary1-র কী-মান জোড়ার সংখ্যা = " + dict1.Count);
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 কী-মান জোড়া...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Dictionary2-র কী-মান জোড়ার সংখ্যা = " + dict2.Count);
}
}
আউটপুট
ListDictionary1 উপাদান... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing Dictionary1-র কী-মান জোড়ার সংখ্যা = 5 ListDictionary2 কী-মান জোড়া... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Dictionary2-র কী-মান জোড়ার সংখ্যা = 6
ব্যাখ্যা: Count প্রোপার্টি O(1) সময়ে জোড়ার সংখ্যা দেয়। Equals() মেথড রেফারেন্স সমতা চেক করে (কন্টেন্ট সমতা নয়)। Clear() কল করলে সব এন্ট্রি মুছে যায় এবং কাউন্ট ০ হয়। ListDictionary ১০টি বা তার কম আইটেমের জন্য দ্রুত, বড় ডেটার জন্য HybridDictionary বা Hashtable ভালো।