ListDictionary.Add(key, value) মেথড ব্যবহার করে লিস্ট ডিকশনারিতে কী/মান জোড়া যোগ করা যায়। ListDictionary একটি সিঙ্গলি-লিঙ্কড লিস্ট বেসড ইমপ্লিমেন্টেশন যা ছোট ডেটাসেট (সাধারণত ১০-এর নিচে) এর জন্য দক্ষ। এটি সন্নিবেশ ক্রম রক্ষা করে এবং IDictionary বাস্তবায়ন করে।
উদাহরণ ১: বেসিক Add এবং IDictionaryEnumerator ব্যবহার
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
Console.WriteLine("ListDictionary কী-মান জোড়া...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
আউটপুট
ListDictionary কী-মান জোড়া... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five
উদাহরণ ২: কী সংগ্রহ (Keys প্রপার্টি) প্রিন্ট করা
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary listDict = new ListDictionary();
listDict.Add("1", 100);
listDict.Add("2", 200);
listDict.Add("3", 300);
listDict.Add("4", 400);
listDict.Add("5", 500);
listDict.Add("6", 600);
listDict.Add("7", 700);
listDict.Add("8", 800);
listDict.Add("9", 900);
listDict.Add("10", 1000);
ICollection col = listDict.Keys;
Console.WriteLine("সব কী প্রদর্শন করা হচ্ছে...");
foreach(String s in col) {
Console.WriteLine(s);
}
}
}
আউটপুট
সব কী প্রদর্শন করা হচ্ছে... 1 2 3 4 5 6 7 8 9 10
দ্রষ্টব্য: ListDictionary O(n) লুকআপ প্রদান করে। ১০-র বেশি উপাদান হলে HybridDictionary বা Hashtable ভালো পারফর্ম করে। আধুনিক C#-এ Dictionary<TKey, TValue> ব্যবহার করা পছন্দনীয়।