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

C# में Dictionary के माध्यम से पुनरावृति करने वाला एन्युमरेटर प्राप्त करें

C# में Dictionary के माध्यम से पुनरावृति करने के लिए GetEnumerator() मेथड का उपयोग करके एक IDictionaryEnumerator प्राप्त किया जा सकता है। यह एन्युमरेटर कुंजी-मान जोड़े पर पुनरावृति करने की अनुमति देता है।

उदाहरण 1

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

public class Demo {
    public static void Main() {
        Dictionary dict = new Dictionary();
        dict.Add(100, "Laptop");
        dict.Add(150, "Desktop");
        dict.Add(200, "Earphone");
        dict.Add(300, "Tablet");
        dict.Add(500, "Speakers");
        dict.Add(750, "HardDisk");
        dict.Add(1000, "SSD");

        IDictionaryEnumerator demoEnum = dict.GetEnumerator();
        Console.WriteLine("एन्युमरेटर कुंजी-मान जोड़े पर पुनरावृति कर रहा है...");
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
    }
}

आउटपुट

एन्युमरेटर कुंजी-मान जोड़े पर पुनरावृति कर रहा है...
Key = 100, Value = Laptop
Key = 150, Value = Desktop
Key = 200, Value = Earphone
Key = 300, Value = Tablet
Key = 500, Value = Speakers
Key = 750, Value = HardDisk
Key = 1000, Value = SSD

उदाहरण 2

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

public class Demo {
    public static void Main() {
        Dictionary dict = new Dictionary();
        dict.Add("One", "Laptop");
        dict.Add("Two", "Desktop");
        dict.Add("Three", "Earphone");
        dict.Add("Four", "Tablet");
        dict.Add("Five", "Speakers");
        dict.Add("Six", "HardDisk");
        dict.Add("Seven", "SSD");
        dict.Add("Eight", "Keyboard");
        dict.Add("Nine", "Mouse");

        IDictionaryEnumerator demoEnum = dict.GetEnumerator();
        Console.WriteLine("एन्युमरेटर कुंजी-मान जोड़े पर पुनरावृति कर रहा है...");
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
    }
}

आउटपुट

एन्युमरेटर कुंजी-मान जोड़े पर पुनरावृति कर रहा है...
Key = One, Value = Laptop
Key = Two, Value = Desktop
Key = Three, Value = Earphone
Key = Four, Value = Tablet
Key = Five, Value = Speakers
Key = Six, Value = HardDisk
Key = Seven, Value = SSD
Key = Eight, Value = Keyboard
Key = Nine, Value = Mouse
  1. - C# में StringCollection के माध्यम से पुनरावृति करने वाला एन्युमरेटर प्राप्त करें

    C# में StringCollection के माध्यम से पुनरावृति करने के लिए GetEnumerator() मेथड का उपयोग करके एक StringEnumerator प्राप्त किया जा सकता है। यह एन्युमरेटर स्ट्रिंग तत्वों पर पुनरावृति करने की अनुमति देता है। उदाहरण 1 using System; using System.Collections.Specialized; public class Demo { public s

  1. - C# में केस-इनसेंसिटिव Dictionary

    तुलना करने के लिए, केस को नजरअंदाज करते हुए, केस-इनसेंसिटिव Dictionary का उपयोग करें।Dictionary घोषित करते समय, केस-इनसेंसिटिव Dictionary प्राप्त करने के लिए निम्नलिखित प्रॉपर्टी सेट करें:StringComparer.OrdinalIgnoreCaseइसे इस तरह जोड़ें:Dictionary <string, int> dict = new Dictionary <strin

  1. - C# में Dictionary से कुंजियों (Keys) की सूची प्राप्त करना

    C# में Dictionary<TKey, TValue> से सभी कुंजियों (keys) को एक सूची (List<TKey>) के रूप में प्राप्त करने के लिए Keys प्रॉपर्टी का उपयोग करके कंस्ट्रक्टर या ToList() मेथड का उपयोग किया जाता है। उदाहरण using System; using System.Collections.Generic; public class Demo { public static v