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

C# में SortedDictionary.Remove() मेथड - कुंजी द्वारा तत्व हटाएं

परिचय

C# में SortedDictionary.Remove() मेथड SortedDictionary<TKey, TValue> से निर्दिष्ट कुंजी वाले तत्व को हटाता है। यदि कुंजी मिलती है और हटा दी जाती है तो true वापस करता है, अन्यथा false।

सिंटैक्स

public bool Remove(TKey key);

key हटाए जाने वाले तत्व की कुंजी है।

उदाहरण 1: इंटीजर कुंजियों के साथ

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(100, "Mobile");
        sortedDict.Add(200, "Laptop");
        sortedDict.Add(300, "Desktop");
        sortedDict.Add(400, "Speakers");
        sortedDict.Add(500, "Headphone");
        sortedDict.Add(600, "Earphone");

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("\nकुंजी 400 हटाई गई? = " + sortedDict.Remove(400));

        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
        Console.WriteLine("\nकुंजियों की सूची...");
        foreach (int i in keyColl)
            Console.WriteLine(i);
    }
}

आउटपुट

SortedDictionary कुंजी-मान जोड़े...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone

कुंजी 400 हटाई गई? = True

SortedDictionary कुंजी-मान जोड़े...अपडेटेड
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 500, Value = Headphone
Key = 600, Value = Earphone

कुंजियों की सूची...
100
200
300
500
600

उदाहरण 2: स्ट्रिंग मानों के साथ

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(1, "Ultrabook");
        sortedDict.Add(2, "Alienware");
        sortedDict.Add(3, "Notebook");
        sortedDict.Add(4, "Connector");
        sortedDict.Add(5, "Flash Drive");
        sortedDict.Add(6, "SSD");
        sortedDict.Add(7, "HDD");
        sortedDict.Add(8, "Earphone");

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("\nकुंजी 7 हटाई गई? = " + sortedDict.Remove(7));

        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
        Console.WriteLine("\nकुंजियों की सूची...");
        foreach (int i in keyColl)
            Console.WriteLine(i);
    }
}

आउटपुट

SortedDictionary कुंजी-मान जोड़े...
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 7, Value = HDD
Key = 8, Value = Earphone

कुंजी 7 हटाई गई? = True

SortedDictionary कुंजी-मान जोड़े...अपडेटेड
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 8, Value = Earphone

कुंजियों की सूची...
1
2
3
4
5
6
8

मुख्य बिंदु

  • यदि कुंजी मौजूद नहीं है तो false वापस करता है (कोई अपवाद नहीं)।
  • हटाने के बाद शेष तत्व सॉर्टेड क्रम में रहते हैं।
  • समय जटिलता: O(log n) - बाइनरी सर्च ट्री संरचना के कारण।
  • Clear() सभी तत्वों को एक साथ हटाता है।
  1. HTML DOM Select remove() Method - विकल्प हटाएं HTML DOM Select remove() Method - विकल्प हटाएं

    HTML DOM Select remove() Method HTML DOM select remove() method ड्रॉप-डाउन सूची से निर्दिष्ट इंडेक्स पर स्थित विकल्प को हटाता है। यह डायनामिकली विकल्पों को हटाने के लिए उपयोगी है। Syntax selectObject.remove(index) यहां index उस विकल्प का इंडेक्स है जिसे ड्रॉप-डाउन सूची से हटाया जाना है। उदाहरण ड्र

  1. HTML DOM Storage key() मेथड HTML DOM Storage key() मेथड

    HTML DOM Storage key() मेथड का उपयोग स्टोरेज ऑब्जेक्ट में दिए गए इंडेक्स पर की नाम को वापस करने के लिए किया जाता है। इंडेक्स को key() मेथड के पैरामीटर के रूप में पास किया जाता है। स्टोरेज ऑब्जेक्ट सेशन ऑब्जेक्ट या localStorage ऑब्जेक्ट हो सकता है। सिंटैक्स LocalStorage का उपयोग करके Storage key() म

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

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