ListDictionary.CopyTo(Array, int index) মেথড ব্যবহার করে লিস্ট ডিকশনারির কী/মান জোড়াগুলো একটি DictionaryEntry অ্যারেতে কপি করা যায়। ListDictionary ছোট ডিকশনারির জন্য অপ্টিমাইজড (লিঙ্কড লিস্ট বেসড), এবং সন্নিবেশ ক্রম রক্ষা করে।
উদাহরণ ১: পূর্ণ কপি (সূচক ০, অ্যারে আকার = Count)
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add(1, "Harry");
dict.Add(2, "Mark");
dict.Add(3, "John");
dict.Add(4, "Jacob");
dict.Add(5, "Tim");
dict.Add(6, "Sam");
dict.Add(7, "Tom");
dict.Add(8, "Kevin");
Console.WriteLine("ListDictionary উপাদানসমূহ...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
DictionaryEntry[] dictArr = new DictionaryEntry[dict.Count];
Console.WriteLine("\nঅ্যারে ইনস্ট্যান্সে কপি করা হচ্ছে...");
dict.CopyTo(dictArr, 0);
for (int i = 0; i < dictArr.Length; i++) {
Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
}
}
}
আউটপুট
ListDictionary উপাদানসমূহ... 1 Harry 2 Mark 3 John 4 Jacob 5 Tim 6 Sam 7 Tom 8 Kevin অ্যারে ইনস্ট্যান্সে কপি করা হচ্ছে... Key = 1, Value = Harry Key = 2, Value = Mark Key = 3, Value = John Key = 4, Value = Jacob Key = 5, Value = Tim Key = 6, Value = Sam Key = 7, Value = Tom Key = 8, Value = Kevin
উদাহরণ ২: বড় অ্যারেতে অফসেট ৫ সহ কপি
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add(1, "Harry");
dict.Add(2, "Mark");
dict.Add(3, "John");
dict.Add(4, "Jacob");
dict.Add(5, "Tim");
Console.WriteLine("ListDictionary উপাদানসমূহ...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
DictionaryEntry[] dictArr = new DictionaryEntry[10];
Console.WriteLine("\nঅ্যারে ইনস্ট্যান্সে কপি করা হচ্ছে...");
dict.CopyTo(dictArr, 5); // সূচক ৫ থেকে শুরু
for (int i = 0; i < dictArr.Length; i++) {
Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
}
}
}
আউটপুট
ListDictionary উপাদানসমূহ... 1 Harry 2 Mark 3 John 4 Jacob 5 Tim অ্যারে ইনস্ট্যান্সে কপি করা হচ্ছে... Key = , Value = Key = , Value = Key = , Value = Key = , Value = Key = , Value = Key = 1, Value = Harry Key = 2, Value = Mark Key = 3, Value = John Key = 4, Value = Jacob Key = 5, Value = Tim
দ্রষ্টব্য: ListDictionary ছোট ডেটাসেটের জন্য দক্ষ (O(n) লুকআপ)। ১০-র বেশি উপাদান হলে HybridDictionary বা Hashtable ভালো পারফর্ম করে। আধুনিক C#-এ Dictionary<TKey, TValue> ব্যবহার করা পছন্দনীয়।