C#-এ OrderedDictionary.Insert(int index, object key, object value) মেথড নির্দিষ্ট ইনডেক্সে কী/ভ্যালু জোড়া ইনসার্ট করে। এটি ইনসার্শন অর্ডার মেইনটেইন করে এবং কী দ্বারা লুকআপ ও ইনডেক্স 의한 অ্যাক্সেস দুটোই সাপোর্ট করে।
উদাহরণ ১: OrderedDictionary-এ ইনসার্ট
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
dict.Add("6", "Six");
dict.Add("7", "Seven");
dict.Add("8", "Eight");
Console.WriteLine("এলিমেন্টসমূহ...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
dict.Insert(7, "15", "Fifteen");
Console.WriteLine("এলিমেন্টসমূহ (আপডেটেড)...");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
আউটপুট
এলিমেন্টসমূহ... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Key = 7, Value = Seven Key = 8, Value = Eight এলিমেন্টসমূহ (আপডেটেড)... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Key = 7, Value = Seven Key = 15, Value = Fifteen Key = 8, Value = Eight
উদাহরণ ২: Add এবং Insert মিশ্রিত ব্যবহার
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Laptop");
dict.Add("B", "Desktop");
Console.WriteLine("এলিমেন্টসমূহ...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
dict.Add("C", "Ultrabook");
dict.Add("D", "Alienware");
Console.WriteLine("\nএলিমেন্টসমূহ (আপডেটেড)...");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
dict.Insert(2, "K", "Speakers");
Console.WriteLine("\nএলিমেন্টসমূহ (আপডেটেড)...");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
আউটপুট
এলিমেন্টসমূহ... Key = A, Value = Laptop Key = B, Value = Desktop এলিমেন্টসমূহ (আপডেটেড)... Key = A, Value = Laptop Key = B, Value = Desktop Key = C, Value = Ultrabook Key = D, Value = Alienware এলিমেন্টসমূহ (আপডেটেড)... Key = A, Value = Laptop Key = B, Value = Desktop Key = K, Value = Speakers Key = C, Value = Ultrabook Key = D, Value = Alienware
ব্যাখ্যা: Insert নির্দিষ্ট পজিশনে কী/ভ্যালু যোগ করে। Add শেষে যোগ করে। IDictionaryEnumerator কী/ভ্যালু উভয় একসাথে অ্যাক্সেস করতে দেয়। OrderedDictionary ছোট ডেটাসেটের জন্য উপযোগী।