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

C# में Dictionary.Clear मेथड


C# में Dictionary.Clear() मेथड Dictionary<TKey,TValue> से सभी कुंजी/मान जोड़े को हटा देता है।

सिंटैक्स

public void Clear();

आइए Dictionary.Clear() मेथड को लागू करने का एक उदाहरण देखते हैं -

उदाहरण

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "John"); 
        dict.Add("Two", "Tom"); 
        dict.Add("Three", "Jacob"); 
        dict.Add("Four", "Kevin"); 
        dict.Add("Five", "Nathan"); 
        Console.WriteLine("Count of elements = "+dict.Count); 
        Console.WriteLine("\nKey/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
        dict.Clear(); 
        Console.WriteLine("Cleared Key/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
        Console.WriteLine("Count of elements now = "+dict.Count); 
    } 
}

आउटपुट

यह निम्नलिखिक आउटपुट देगा -

Count of elements = 5 Key/value pairs... Key = One, Value = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan Cleared Key/value pairs... Count of elements now = 0

आइए Dictionary.Clear() मेथड को लागू करने का एक और उदाहरण देखते हैं -

उदाहरण

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "John"); 
        dict.Add("Two", "Tom"); 
        dict.Add("Three", "Jacob"); 
        dict.Add("Four", "Kevin"); 
        dict.Add("Five", "Nathan"); 
        Console.WriteLine("Count of elements = "+dict.Count); 
        dict.Add("Six", "Anne"); 
        dict.Add("Seven", "Katoe"); 
        Console.WriteLine("Count of elements (updated) = "+dict.Count); 
        Console.WriteLine("Key/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
        dict.Clear(); 
        Console.WriteLine("Cleared Key/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
    } 
}

आउटपुट

यह निम्नलिखिक आउटपुट देगा -

Count of elements = 5 Count of elements (updated) = 7 Key/value pairs... Key = One, Value = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan Key = Six, Value = Anne Key = Seven, Value = Katoe Cleared Key/value pairs...
  1. - जावास्क्रिप्ट का उपयोग करके डिक्शनरी क्लियर करना

    हम एक clear() फ़ंक्शन लागू करेंगे जो कंटेनर की सामग्री को बस साफ कर देता है। उदाहरण के लिए, उदाहरण clear() { this.container = {} } आप इसका परीक्षण इस तरह कर सकते हैं: उदाहरण const myMap = new MyMap(); myMap.put(key1, value1); myMap.put(key2, value2); myMap.display(); myMap.clear(); myMap.displa

  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