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

C# में Dictionary.Remove() मेथड


C# में Dictionary.Remove() मेथड का उपयोग Dictionary<TKey, TValue> से निर्दिष्ट कुंजी के साथ मान को हटाने के लिए किया जाता है।

सिंटैक्स

निम्नलिखित सिंटैक्स है:

public bool Remove (TKey key);

यहाँ, key वह कुंजी है जिसे हटाना है।

उदाहरण

आइए अब Dictionary.Remove() मेथड को लागू करने का एक उदाहरण देखें:

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "Kagido"); 
        dict.Add("Two", "Ngidi"); 
        dict.Add("Three", "Devillers"); 
        dict.Add("Four", "Smith"); 
        dict.Add("Five", "Warner"); 
        Console.WriteLine("Count of elements = " + dict.Count); 
        Console.WriteLine("Removing some keys..."); 
        dict.Remove("Four"); 
        dict.Remove("Five"); 
        Console.WriteLine("Count of elements (updated) = " + dict.Count); 
        Console.WriteLine("\nKey/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
        Console.Write("\nAll the keys..\n"); 
        Dictionary<string, string>.KeyCollection allKeys = dict.Keys; 
        foreach(string str in allKeys){ 
            Console.WriteLine("Key = {0}", str); 
        } 
    } 
}

आउटपुट

यह निम्नलिखित आउटपुट उत्पन्न करेगा:

Count of elements = 5 
Removing some keys... 
Count of elements (updated) = 3 
Key/value pairs... 
Key = One, Value = Kagido 
Key = Two, Value = Ngidi 
Key = Three, Value = Devillers 
All the keys.. 
Key = One 
Key = Two 
Key = Three

उदाहरण

आइए अब Dictionary.Remove() मेथड को लागू करने का एक और उदाहरण देखें:

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "Kagido"); 
        dict.Add("Two", "Ngidi"); 
        dict.Add("Three", "Devillers"); 
        dict.Add("Four", "Smith"); 
        dict.Add("Five", "Warner"); 
        Console.WriteLine("Count of elements = " + dict.Count); 
        Console.Write("\nAll the keys..\n"); 
        Dictionary<string, string>.KeyCollection allKeys = dict.Keys; 
        foreach(string str in allKeys){ 
            Console.WriteLine("Key = {0}", str); 
        } 
        Console.WriteLine("Removing some keys..."); 
        dict.Remove("Four"); 
        dict.Remove("Five"); 
        Console.WriteLine("Count of elements (updated) = " + dict.Count); 
        Console.Write("\nAll the keys..updated\n"); 
        foreach(string str in allKeys){ 
            Console.WriteLine("Key = {0}", str); 
        } 
        Console.WriteLine("\nKey/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
    } 
}

आउटपुट

यह निम्नलिखित आउटपुट उत्पन्न करेगा:

Count of elements = 5 
All the keys.. 
Key = One 
Key = Two 
Key = Three 
Key = Four 
Key = Five 
Removing some keys... 
Count of elements (updated) = 3 
All the keys..updated 
Key = One 
Key = Two 
Key = Three 
Key/value pairs... 
Key = One, Value = Kagido 
Key = Two, Value = Ngidi 
Key = Three, Value = Devillers

  1. - Python - डिक्शनरी से की (key) को हटाने के तरीके

    डिक्शनरी का उपयोग दिन-प्रतिदिन की प्रोग्रामिंग, वेब डेवलपमेंट और AI/ML प्रोग्रामिंग जैसे कई व्यावहारिक अनुप्रयोगों में किया जाता है, जो इसे समग्र रूप से एक उपयोगी कंटेनर बनाता है। इसलिए, डिक्शनरी उपयोग से संबंधित विभिन्न कार्यों को प्राप्त करने के तरीकों को जानना हमेशा एक अतिरिक्त लाभ होता है। उदाहर

  1. - पायथन डिक्शनरी से कुंजी कैसे हटाएं?

    पायथन का del कीवर्ड लगभग किसी भी ऑब्जेक्ट के लिए उपयोग किया जाता है। डिक्शनरी से किसी विशेष आइटम को हटाने के लिए, del स्टेटमेंट को की क्लॉज प्रदान करें।>>> D1 = {1: a, 2: b, 3: c, x: 1, y: 2, z: 3}\n>>> del D1[x]\n>>> D1\n{1: a, 2: b, 3: c, y: 2, z: 3}कुंजी-मान जोड़ी को हट

  1. - कॉर्पोरेट नेटवर्क से Skeleton Key मैलवेयर हटाने के लिए चरण-दर-चरण गाइड

    यह उच्च समय है कि आप अपने कॉर्पोरेट नेटवर्क पर मजबूत सुरक्षा और निवारक उपाय लागू करें। क्यों? ऐसा इसलिए है क्योंकि आज कॉर्पोरेट नेटवर्क पर हमला करने वाला एक ज्ञात मैलवेयर है। इसे Skeleton Key कहा जाता है। Skeleton Key क्या है? Skeleton Key एक खतरनाक ट्रोजन है जो कॉर्पोरेट नेटवर्क से संबंधित असुरक्षि