C# में OrderedDictionary वर्ग की RemoveAt(int index) विधि का उपयोग करके निर्दिष्ट सूचकांक पर स्थित प्रविष्टि को हटाया जा सकता है। चूंकि OrderedDictionary सम्मिलन क्रम बनाए रखता है, सूचकांक तत्वों के जोड़े जाने के क्रम से मेल खाता है।
उदाहरण 1: सूचकांक 4 पर प्रविष्टि हटाना
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.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
उदाहरण 2: एकाधिक सूचकांकों पर प्रविष्टियाँ हटाना
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);
dict.RemoveAt(1);
dict.RemoveAt(2);
Console.WriteLine("OrderedDictionary में तत्वों की संख्या (अपडेटेड) = " + dict.Count);
}
}
आउटपुट
OrderedDictionary तत्व... 1 AB 2 CD 3 MN 4 PQ OrderedDictionary में तत्वों की संख्या = 4 OrderedDictionary में तत्वों की संख्या (अपडेटेड) = 2
मुख्य बिंदु
RemoveAt(index)शून्य-आधारित सूचकांक पर प्रविष्टि हटाता है- सूचकांक सम्मिलन क्रम पर आधारित होता है
- अमान्य सूचकांक पर
ArgumentOutOfRangeExceptionफेंकता है Remove(key)से कुंजी द्वारा भी हटा सकते हैं- हटाने के बाद बाद के तत्वों के सूचकांक स्वचालित रूप से घट जाते हैं