C#-এ OrderedDictionary ক্লাসের Remove(object key) মেথড ব্যবহার করে নির্দিষ্ট কী-এর এন্ট্রি মুছে ফেলা যায়। OrderedDictionary ইনসারশন অর্ডার বজায় রাখে এবং কী-বেসড ও ইন্ডেক্স-বেসড (RemoveAt) দুভায় রিমুভ সাপোর্ট করে।
উদাহরণ ১: "E" কী মুছে ফেলা
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" কী মুছে ফেলা
dict.Remove("E");
Console.WriteLine("OrderedDictionary-র উপাদানের সংখ্যা (আপডেটেড) = " + dict.Count);
}
}
আউটপুট
OrderedDictionary উপাদান... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear OrderedDictionary-র উপাদানের সংখ্যা = 6 OrderedDictionary-র উপাদানের সংখ্যা (আপডেটেড) = 5
উদাহরণ ২: "C" এবং "D" কী মুছে ফেলা
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "One");
dict.Add("B", "Two");
dict.Add("C", "Three");
dict.Add("D", "Four");
Console.WriteLine("OrderedDictionary উপাদান...");
foreach (DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("OrderedDictionary-র উপাদানের সংখ্যা = " + dict.Count);
dict.Remove("C");
dict.Remove("D");
Console.WriteLine("OrderedDictionary-র উপাদানের সংখ্যা (আপডেটেড) = " + dict.Count);
}
}
আউটপুট
OrderedDictionary উপাদান... A One B Two C Three D Four OrderedDictionary-র উপাদানের সংখ্যা = 4 OrderedDictionary-র উপাদানের সংখ্যা (আপডেটেড) = 2
ব্যাখ্যা: Remove(key) মেথড কী খুঁজে পেলে সংশ্লিষ্ট এন্ট্রিটি মুছে ফেলে এবং ইনসারশন অর্ডার বজায় রাখে (বাকি উপাদানগুলো শিফট করে)। কী না পেলে কিছুই করে না। OrderedDictionary হ্যাশ টেবিল + অ্যারে লিস্ট হাইব্রিড। এটি লিগ্যাসি নন-জেনেরিক ক্লাস; নতুন প্রজেক্টে কাস্টম অর্ডারড ডিকশনারি বানাতে Dictionary<TKey, TValue> + List<TKey> বা থার্ড-পার্টি লাইব্রেরি (যেমন OrderedDictionary NuGet প্যাকেজ) ব্যবহার করা ভালো।