C# में OrderedDictionary वर्ग की Remove(key) विधि का उपयोग करके निर्दिष्ट कुंजी और उससे जुड़े मान को हटाया जा सकता है। यह विधि कुंजी मौजूद होने पर प्रविष्टि हटा देती है। OrderedDictionary सम्मिलन क्रम को बनाए रखता है।
उदाहरण 1: कुंजी "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);
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
उदाहरण 2: एकाधिक कुंजियाँ क्रमिक रूप से हटाना
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)निर्दिष्ट कुंजी और उसका मान हटाता है- कुंजी नहीं मिलने पर कोई अपवाद नहीं फेंकता
RemoveAt(index)से सूचकांक द्वारा भी हटा सकते हैं- हटाने के बाद सम्मिलन क्रम में शेष तत्व बने रहते हैं
OrderedDictionaryगैर-जेनेरिक है; आधुनिक कोड मेंDictionary<TKey, TValue>के साथ क्रम बनाए रखने के लिए अन्य तरीके उपयोग करें