SortedList.CopyTo() মেথড ব্যবহার করে SortedList-এর সব এন্ট্রি (DictionaryEntry অবজেক্ট হিসেবে) একটি একমাত্রি অ্যারেতে কপি করা যায়। লক্ষ্য অ্যারেতে সূচক নির্দিষ্ট করে কপি শুরু করার অবস্থান নিয়ন্ত্রণ করা যায়।
উদাহরণ ১: অফসেট সহ অ্যারেতে কপি
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList list = new SortedList();
list.Add("1", "AB");
list.Add("2", "CD");
list.Add("3", "EF");
list.Add("4", "GH");
list.Add("5", "IJ");
list.Add("6", "JK");
list.Add("7", "KL");
list.Add("8", "LM");
Console.WriteLine("SortedList উপাদানসমূহ...");
foreach(DictionaryEntry d in list) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("\nঅ্যারে অবজেক্টে কপি করা হলো...");
DictionaryEntry[] dictArr = new DictionaryEntry[10];
list.CopyTo(dictArr, 2); // সূচক ২ থেকে শুরু
for (int i = 0; i < dictArr.Length; i++) {
Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
}
}
}
আউটপুট
SortedList উপাদানসমূহ... Key = 1, Value = AB Key = 2, Value = CD Key = 3, Value = EF Key = 4, Value = GH Key = 5, Value = IJ Key = 6, Value = JK Key = 7, Value = KL Key = 8, Value = LM অ্যারে অবজেক্টে কপি করা হলো... Key = , Value = Key = , Value = Key = 1, Value = AB Key = 2, Value = CD Key = 3, Value = EF Key = 4, Value = GH Key = 5, Value = IJ Key = 6, Value = JK Key = 7, Value = KL Key = 8, Value = LM
উদাহরণ ২: সূচক ০ থেকে কপি
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList list = new SortedList();
list.Add("1", "Katie");
list.Add("2", "Andy");
list.Add("3", "Mark");
list.Add("4", "Gary");
list.Add("5", "Sam");
Console.WriteLine("SortedList উপাদানসমূহ...");
foreach(DictionaryEntry d in list) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("\nঅ্যারে অবজেক্টে কপি করা হলো...");
DictionaryEntry[] dictArr = new DictionaryEntry[5];
list.CopyTo(dictArr, 0);
for (int i = 0; i < dictArr.Length; i++) {
Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
}
}
}
আউটপুট
SortedList উপাদানসমূহ... Key = 1, Value = Katie Key = 2, Value = Andy Key = 3, Value = Mark Key = 4, Value = Gary Key = 5, Value = Sam অ্যারে অবজেক্টে কপি করা হলো... Key = 1, Value = Katie Key = 2, Value = Andy Key = 3, Value = Mark Key = 4, Value = Gary Key = 5, Value = Sam
মূল বিন্দু: লক্ষ্য অ্যারের আকার সোর্স SortedList-এর Count-এর চেয়ে বড় হলে, الإضافي উপাদানগুলো ডিফল্ট মান (null/empty) ধারণ করে।