OrderedDictionary.CopyTo(Array, int index) মেথড ব্যবহার করে অর্ডার্ড ডিকশনারির কী/মান জোড়াগুলো একটি DictionaryEntry অ্যারেতে কপি করা যায়। OrderedDictionary সন্নিবেশ ক্রম (insertion order) রক্ষা করে, কিন্তু CopyTo করার সময় এনামারেশন ক্রমটি রিভার্স ক্রমে হতে পারে (ইমপ্লিমেন্টেশন-নির্ভর)।
উদাহরণ ১: পূর্ণ কপি (সূচক ০, অ্যারে আকার = Count)
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add(1, "Harry");
dict.Add(2, "Mark");
dict.Add(3, "John");
dict.Add(4, "Jacob");
dict.Add(5, "Tim");
Console.WriteLine("OrderedDictionary উপাদানসমূহ...");
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);
}
}
}
আউটপুট
OrderedDictionary উপাদানসমূহ... 1 Harry 2 Mark 3 John 4 Jacob 5 Tim অ্যারে ইনস্ট্যান্সে কপি করা হচ্ছে... Key = 5, Value = Tim Key = 4, Value = Jacob Key = 3, Value = John Key = 2, Value = Mark Key = 1, Value = Harry
উদাহরণ ২: বড় অ্যারেতে কপি (সূচক ০, অ্যারে আকার ৫, ডিকশনারি আকার ২)
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add(1, 10);
dict.Add(2, 20);
Console.WriteLine("OrderedDictionary উপাদানসমূহ...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
DictionaryEntry[] dictArr = new DictionaryEntry[5];
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);
}
}
}
আউটপুট
OrderedDictionary উপাদানসমূহ... 1 10 2 20 অ্যারে ইনস্ট্যান্সে কপি করা হচ্ছে... Key = 2, Value = 20 Key = 1, Value = 10 Key = , Value = Key = , Value = Key = , Value =
দ্রষ্টব্য: OrderedDictionary এনামারেশন সন্নিবেশ ক্রমে, কিন্তু CopyTo() আউটপুটে রিভার্স ক্রম দেখাচ্ছে (শেষ সন্নিবেশিত প্রথমে)। এটি ইমপ্লিমেন্টেশন ডিটেইল। আধুনিক বিকল্প: Dictionary<TKey, TValue> (ক্রম নিশ্চিত নয়) বা ImmutableDictionary।