C#-এ OrderedDictionary ক্লাসের RemoveAt(int index) মেথড ব্যবহার করে নির্দিষ্ট ইনডেক্সে অবস্থিত এন্ট্রি মুছে ফেলা যায়। OrderedDictionary ইনসারশন অর্ডার বজায় রাখে, তাই ইনডেক্স ০ হলো প্রথম ইনসার্ট করা আইটেম। এটি কী-বেসড Remove(key) মেথডও সাপোর্ট করে।
উদাহরণ ১: ইনডেক্স ৪ (৫ম আইটেম) মুছে ফেলা
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
dict.Add("C", "Smart Wearables");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("OrderedDictionary উপাদান...");
foreach (DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("OrderedDictionary-র উপাদানের সংখ্যা = " + dict.Count);
// ইনডেক্স ৪ (E - Clothing) মুছে ফেলা
dict.RemoveAt(4);
Console.WriteLine("OrderedDictionary-র উপাদানের সংখ্যা (আপডেটেড) = " + dict.Count);
}
}
আউটপুট
OrderedDictionary উপাদান... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear OrderedDictionary-র উপাদানের সংখ্যা = 6 OrderedDictionary-র উপাদানের সংখ্যা (আপডেটেড) = 5
উদাহরণ ২: একাধিক ইনডেক্স থেকে মুছে ফেলা
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "AB");
dict.Add("2", "CD");
dict.Add("3", "MN");
dict.Add("4", "PQ");
Console.WriteLine("OrderedDictionary উপাদান...");
foreach (DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("OrderedDictionary-র উপাদানের সংখ্যা = " + dict.Count);
// ইনডেক্স ১ (CD) মুছে ফেলা
dict.RemoveAt(1);
// আপডেটেড লিস্টে ইনডেক্স ২ (PQ) মুছে ফেলা
dict.RemoveAt(2);
Console.WriteLine("OrderedDictionary-র উপাদানের সংখ্যা (আপডেটেড) = " + dict.Count);
}
}
আউটপুট
OrderedDictionary উপাদান... 1 AB 2 CD 3 MN 4 PQ OrderedDictionary-র উপাদানের সংখ্যা = 4 OrderedDictionary-র উপাদানের সংখ্যা (আপডেটেড) = 2
ব্যাখ্যা: RemoveAt(index) মেথড নির্দিষ্ট ইনডেক্সের এন্ট্রি মুছে ফেলে এবং বাকি উপাদানগুলো শিফট করে। ইনডেক্স ০-বেসড। অবৈধ ইনডেক্সের জন্য ArgumentOutOfRangeException থ্রো করে। OrderedDictionary ইন্ডেক্স এবং কী দুভায় অ্যাক্সেস সাপোর্ট করে। এটি হ্যাশ টেবিল + অ্যারে লিস্ট হাইব্রিড। নতুন প্রজেক্টে Dictionary<TKey, TValue> + List<TKey> বা SortedDictionary বিবেচনা করা উচিত।