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

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

उदाहरण

SortedDictionary<TKey, TValue> की GetEnumerator() मेथड एक IDictionaryEnumerator लौटाती है जिसका उपयोग की-वैल्यू जोड़े के माध्यम से पुनरावृत्ति के लिए किया जा सकता है।

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

public class Demo {
    public static void Main() {
        SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
        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);
    }
}

आउटपुट

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

एक और उदाहरण

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

public class Demo {
    public static void Main() {
        SortedDictionary<int, int> sortedDict = new SortedDictionary<int, int>();
        sortedDict.Add(100, 1);
        sortedDict.Add(200, 2);
        sortedDict.Add(300, 3);
        sortedDict.Add(400, 4);
        sortedDict.Add(500, 5);
        sortedDict.Add(600, 6);
        sortedDict.Add(700, 7);
        sortedDict.Add(800, 8);
        sortedDict.Add(900, 9);
        sortedDict.Add(1000, 10);

        Console.WriteLine("SortedDictionary की-वैल्यू जोड़े...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
    }
}

आउटपुट

SortedDictionary की-वैल्यू जोड़े...
Key = 100, Value = 1
Key = 200, Value = 2
Key = 300, Value = 3
Key = 400, Value = 4
Key = 500, Value = 5
Key = 600, Value = 6
Key = 700, Value = 7
Key = 800, Value = 8
Key = 900, Value = 9
Key = 1000, Value = 10
  1. - C# में Dictionary के माध्यम से पुनरावृति करने वाला एन्युमरेटर प्राप्त करें

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

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

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

  1. - C# में ड्राइव फॉर्मेट (फाइल सिस्टम) प्राप्त करना

    C# में किसी ड्राइव का फाइल सिस्टम फॉर्मेट (जैसे NTFS, FAT32, exFAT, ext4 आदि) जानने के लिए DriveInfo.DriveFormat प्रॉपर्टी का उपयोग किया जाता है। उदाहरण using System; using System.IO; public class Demo { public static void Main() { try { // C: ड्राइव के लिए Dri