C# में StringDictionary वर्ग की Remove(key) विधि का उपयोग करके निर्दिष्ट कुंजी और उससे जुड़े मान को हटाया जा सकता है। यह विधि कुंजी मौजूद होने पर प्रविष्टि हटा देती है। StringDictionary केवल स्ट्रिंग कुंजी और मान स्वीकार करता है, और कुंजियाँ केस-असंवेदनशील होती हैं।
उदाहरण 1: दो StringDictionary से कुंजी हटाना
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);
}
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("कुंजी = " + d.Key + ", मान = " + 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 कुंजी-मान जोड़े... कुंजी = 1, मान = A कुंजी = 2, मान = B कुंजी = 3, मान = C कुंजी = 4, मान = D कुंजी = 5, मान = E
उदाहरण 2: एकाधिक कुंजियाँ क्रमिक रूप से हटाना
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>का उपयोग करने की अनुशंसा की जाती है