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

C# में ListDictionary से निर्दिष्ट कुंजी वाली प्रविष्टि हटाएं

C# में ListDictionary वर्ग की Remove(key) विधि का उपयोग करके निर्दिष्ट कुंजी और उससे जुड़े मान को हटाया जा सकता है। यह विधि कुंजी मौजूद होने पर प्रविष्टि हटा देती है। ListDictionary छोटे संग्रह के लिए अनुकूलित है।

उदाहरण 1: दो ListDictionary से कुंजी हटाना

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        ListDictionary dict1 = new ListDictionary();
        dict1.Add("A", "Books");
        dict1.Add("B", "Electronics");
        dict1.Add("C", "Smart Wearables");
        dict1.Add("D", "Pet Supplies");
        dict1.Add("E", "Clothing");
        dict1.Add("F", "Footwear");
        
        Console.WriteLine("ListDictionary1 तत्व...");
        foreach(DictionaryEntry d in dict1) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        ListDictionary dict2 = new ListDictionary();
        dict2.Add("1", "One");
        dict2.Add("2", "Two");
        dict2.Add("3", "Three");
        dict2.Add("4", "Four");
        dict2.Add("5", "Five");
        dict2.Add("6", "Six");
        
        Console.WriteLine("\nListDictionary2 तत्व...");
        foreach(DictionaryEntry d in dict2) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("डिक्शनरी 2 में कुंजी-मान जोड़ों की संख्या = " + dict2.Count);
        
        dict2.Remove("4");
        
        Console.WriteLine("डिक्शनरी 2 में कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + dict2.Count);
    }
}

आउटपुट

ListDictionary1 तत्व...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

ListDictionary2 तत्व...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
डिक्शनरी 2 में कुंजी-मान जोड़ों की संख्या = 6
डिक्शनरी 2 में कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 5

उदाहरण 2: एकाधिक कुंजियाँ क्रमिक रूप से हटाना

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        ListDictionary dict = new ListDictionary();
        dict.Add("1", "One");
        dict.Add("2", "Two");
        dict.Add("3", "Three");
        dict.Add("4", "Four");
        dict.Add("5", "Five");
        dict.Add("6", "Six");
        
        Console.WriteLine("ListDictionary तत्व...");
        foreach(DictionaryEntry d in dict) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("डिक्शनरी में कुंजी-मान जोड़ों की संख्या = " + dict.Count);
        
        dict.Remove("2");
        dict.Remove("3");
        dict.Remove("4");
        
        Console.WriteLine("डिक्शनरी में कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + dict.Count);
    }
}

आउटपुट

ListDictionary तत्व...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
डिक्शनरी में कुंजी-मान जोड़ों की संख्या = 6
डिक्शनरी में कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 3

मुख्य बिंदु

  • Remove(key) निर्दिष्ट कुंजी और उसका मान हटाता है
  • कुंजी नहीं मिलने पर कोई अपवाद नहीं फेंकता
  • Count प्रॉपर्टी हटाने के बाद स्वचालित रूप से अपडेट होती है
  • ListDictionary 10 या उससे कम आइटम के लिए अनुकूलित है
  • बड़े संग्रह के लिए HybridDictionary या Hashtable बेहतर हैं
  1. - C# में ListDictionary से सभी एंट्री हटाएं

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

  1. - C# में ArrayList से तत्वों की एक रेंज हटाएं

    उदाहरण ArrayList की RemoveRange() मेथड निर्दिष्ट इंडेक्स से शुरू होने वाले निर्दिष्ट संख्या में तत्वों को हटा देती है। इस उदाहरण में इंडेक्स 3 से 2 तत्व हटाए गए हैं। using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list1 = ne

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

    उदाहरण ArrayList की Clear() मेथड सूची से सभी तत्वों को हटा देती है। इस उदाहरण में तीन ArrayList बनाए गए हैं, तीसरे को दूसरे के बराबर सेट किया गया है, फिर Clear() कॉल करके खाली किया गया है। using System; using System.Collections; public class Demo { public static void Main(String[] args) {