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

C# में SortedDictionary.Clear() मेथड - सभी तत्व हटाएं

परिचय

C# में SortedDictionary.Clear() मेथड SortedDictionary<TKey, TValue> से सभी तत्वों को हटा देता है, जिससे यह खाली हो जाता है। Count प्रॉपर्टी 0 हो जाती है।

सिंटैक्स

public void Clear();

उदाहरण 1: ContainsKey के साथ

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("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);

        Console.WriteLine("\nक्या SortedDictionary में कुंजी 200 है? = " + sortedDict.ContainsKey(200));

        sortedDict.Clear();
        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);
    }
}

आउटपुट

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
SortedDictionary कुंजी-मान जोड़ों की संख्या = 6
क्या SortedDictionary में कुंजी 200 है? = True
SortedDictionary कुंजी-मान जोड़ों की संख्या = 0

उदाहरण 2: जोड़ने के बाद क्लियर

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

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

        sortedDict.Add(100, "Inspiron");
        sortedDict.Add(200, "Alienware");
        sortedDict.Add(300, "Projectors");
        sortedDict.Add(400, "XPS");

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

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);

        sortedDict.Add(800, "Notebook");
        sortedDict.Add(10000, "Bluetooth Speaker");

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

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);

        sortedDict.Clear();
        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
    }
}

आउटपुट

SortedDictionary कुंजी-मान जोड़े...
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
SortedDictionary कुंजी-मान जोड़ों की संख्या = 4

SortedDictionary कुंजी-मान जोड़े...अपडेटेड
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Key = 800, Value = Notebook
Key = 10000, Value = Bluetooth Speaker
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 6
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 0

मुख्य बिंदु

  • सभी कुंजी-मान जोड़े हटा देता है - O(1) ऑपरेशन (बस रूट नोड रीसेट करता है)।
  • क्षमता (आंतरिक ट्री संरचना) बरकरार रह सकती है - पुन: उपयोग के लिए।
  • Count 0 हो जाता है, Keys और Values खाली संग्रह वापस करते हैं।
  • नए SortedDictionary बनाने से अलग - मौजूदा इंस्टेंस पुन: उपयोग योग्य रहता है।
  • थ्रेड-सेफ नहीं - मल्टी-थ्रेडेड पहुंच के लिए लॉकिंग आवश्यक।
  1. HTML Canvas clearRect() मेथड: आयताकार क्षेत्र साफ़ करना HTML Canvas clearRect() मेथड: आयताकार क्षेत्र साफ़ करना

    Canvas clearRect() मेथड clearRect() मेथड HTML Canvas में निर्दिष्ट आयताकार क्षेत्र के पिक्सल्स को साफ़ (clear) करता है, जिससे वह क्षेत्र पारदर्शी (transparent) हो जाता है। यह एनिमेशन, गेम डेवलपमेंट और डायनामिक ग्राफिक्स में फ्रेम दर फ्रेम कैनवास को साफ़ करने के लिए आवश्यक है। सिंटैक्स ctx.clearRect

  1. HTML DOM console.clear() मेथड HTML DOM console.clear() मेथड

    HTML DOM console.clear() मेथड का उपयोग कंसोल को क्लियर करने के लिए किया जाता है। console.clear() मेथड कंसोल में Console was cleared मैसेज भी लिखेगा और सभी पिछले कंसोल कंटेंट को क्लियर कर देगा। यह मेथड सभी ब्राउज़र्स द्वारा पूरी तरह से सपोर्टेड है।सिंटैक्स (Syntax)console.clear() मेथड का सिंटैक्स:con

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

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