Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में OrderedDictionary से निर्दिष्ट सूचकांक पर प्रविष्टि हटाएं

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) से कुंजी द्वारा भी हटा सकते हैं
  • हटाने के बाद बाद के तत्वों के सूचकांक स्वचालित रूप से घट जाते हैं
  1. - C# में SortedSet से निर्दिष्ट आइटम हटाएं

    उदाहरण SortedSet<T> की Remove() मेथड निर्दिष्ट तत्व को सेट से हटा देती है यदि वह मौजूद है। यह मेथड एक बूलियन मान लौटाती है जो दर्शाता है कि तत्व सफलतापूर्वक हटाया गया था या नहीं। using System; using System.Collections.Generic; public class Demo { public static void Main() { Sorted

  1. - C# में OrderedDictionary से सभी तत्व हटाएं

    उदाहरण OrderedDictionary की Clear() मेथड डिक्शनरी से सभी की-वैल्यू जोड़े को हटा देती है। इस उदाहरण में एक OrderedDictionary बनाया गया है, उसमें आइटम जोड़े गए हैं, फिर Clear() कॉल करके खाली किया गया है। using System; using System.Collections; using System.Collections.Specialized; public class Demo {

  1. - Python Pandas - CategoricalIndex से निर्दिष्ट श्रेणियां हटाएं

    Pandas में CategoricalIndex से निर्दिष्ट श्रेणियां हटाने के लिए remove_categories() मेथड का उपयोग करें। आवश्यक लाइब्रेरी इम्पोर्ट करें import pandas as pd CategoricalIndex बनाएं categories पैरामीटर का उपयोग करके श्रेणियां सेट करें। ordered पैरामीटर का उपयोग करके श्रेणी को क्रमबद्ध मानें। catIndex