C#-এ StringDictionary ক্লাসের Remove(string key) মেথড ব্যবহার করে নির্দিষ্ট কী-এর এন্ট্রি মুছে ফেলা যায়। StringDictionary কেস-ইনসেনসিটিভ কী সাপোর্ট করে (কী স্টোর হয় लोয়ারকেসে)। Remove মেথড কী খুঁজে পেলে এন্ট্রিটি মুছে ফেলে, না পেলে কিছুই করে না।
উদাহরণ ১: "D" কী মুছে ফেলা এবং আপডেটেড লিস্ট দেখানো
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 কী-মান জোড়া...");
foreach (DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
// "D" কী মুছে ফেলা
strDict1.Remove("D");
Console.WriteLine("\nStringDictionary1 কী-মান জোড়া (একটি কী রিমুভ করার পর)...");
foreach (DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 কী-মান জোড়া...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
}
}
আউটপুট
StringDictionary1 কী-মান জোড়া... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary1 কী-মান জোড়া (একটি কী রিমুভ করার পর)... a John b Andy c Tim e Kevin f Katie g Brad StringDictionary2 কী-মান জোড়া... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E
উদাহরণ ২: "B" এবং "C" কী মুছে ফেলা
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "One");
strDict1.Add("B", "Two");
strDict1.Add("C", "Three");
strDict1.Add("D", "Four");
strDict1.Add("E", "Five");
Console.WriteLine("StringDictionary1 কী-মান জোড়া...");
foreach (DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
strDict1.Remove("B");
strDict1.Remove("C");
Console.WriteLine("\nStringDictionary1 কী-মান জোড়া (কী রিমুভ করার পর)...");
foreach (DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
আউটপুট
StringDictionary1 কী-মান জোড়া... a One b Two c Three d Four e Five StringDictionary1 কী-মান জোড়া (কী রিমুভ করার পর)... a One d Four e Five
ব্যাখ্যা: Remove(key) কেস-ইনসেনসিটিভভাবে কী খুঁজে বের করে এবং মুছে ফেলে। কী না পেলে এক্সেপশন থ্রো করে না। StringDictionary হ্যাশ টেবিল বেসড। লিগ্যাসি নন-জেনেরিক ক্লাস; নতুন প্রজেক্টে Dictionary<string, string> (কেস-সেনসিটিভ ডিফল্ট, StringComparer.OrdinalIgnoreCase দিয়ে কেস-ইনসেনসিটিভ করা যায়) ব্যবহার করা উচিত।