কম্পিউটার টিউটোরিয়াল

C#-এ StringDictionary-র মাধ্যমে এনুমারেটর পাওয়ার পদ্ধতি

C#-এ StringDictionary ক্লাসের GetEnumerator() মেথড একটি IEnumerator রিটার্ন করে যা কী/ভ্যালু জোড়াগুলোর মাধ্যমে ইটারেট করতে ব্যবহার করা হয়। এটি DictionaryEntry অবজেক্ট হিসেবে প্রতিটি জোড়া প্রদান করে।

উদাহরণ ১: দুইটি StringDictionary ইটারেট করা

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringDictionary strDict1 = new StringDictionary();
        strDict1.Add("A", "John");
        strDict1.Add("B", "Andy");
        strDict1.Add("C", "Tim");
        strDict1.Add("D", "Ryan");
        strDict1.Add("E", "Kevin");
        strDict1.Add("F", "Katie");
        strDict1.Add("G", "Brad");

        Console.WriteLine("StringDictionary1 এলিমেন্টসমূহ...");
        foreach (DictionaryEntry de in strDict1) {
            Console.WriteLine(de.Key + " " + de.Value);
        }

        StringDictionary strDict2 = new StringDictionary();
        strDict2.Add("1", "A");
        strDict2.Add("2", "B");
        strDict2.Add("3", "C");
        strDict2.Add("4", "D");
        strDict2.Add("5", "E");

        Console.WriteLine("\nStringDictionary2 কী-ভ্যালু জোড়াসমূহ...");
        IEnumerator demoEnum = strDict2.GetEnumerator();
        DictionaryEntry d;
        while (demoEnum.MoveNext()) {
            d = (DictionaryEntry)demoEnum.Current;
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
    }
}

আউটপুট

StringDictionary1 এলিমেন্টসমূহ...
A John
B Andy
C Tim
D Ryan
E Kevin
F Katie
G Brad
StringDictionary2 কী-ভ্যালু জোড়াসমূহ...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E

উদাহরণ ২: এনুমারেটর ব্যবহার করে ইটারেশন

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("1", "One");
        strDict.Add("2", "Two");
        strDict.Add("3", "Three");
        strDict.Add("4", "Four");

        Console.WriteLine("StringDictionary কী-ভ্যালু জোড়াসমূহ...");
        IEnumerator demoEnum = strDict.GetEnumerator();
        DictionaryEntry d;
        while (demoEnum.MoveNext()) {
            d = (DictionaryEntry)demoEnum.Current;
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
    }
}

আউটপুট

StringDictionary কী-ভ্যালু জোড়াসমূহ...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four

ব্যাখ্যা: StringDictionary একটি পুরানো কালেকশন ক্লাস (System.Collections.Specialized)। আধুনিক কোডে Dictionary<string, string> ব্যবহার করা ভালো। GetEnumerator() ম্যানুয়াল ইটারেশনের জন্য এনুমারেটর রিটার্ন করে, যদিও foreach ব্যবহার করা সহজ এবং পছন্দনীয়।

  1. C#-এ List-এ ইনুমেরেটর পাওয়া (GetEnumerator)

  2. C#-এ StringDictionary-এ কিগুলোর কালেকশন পাওয়া (Keys প্রপার্টি)

  3. C#-এ Dictionary-এর মাধ্যমে পুনরাবৃত্তি করার জন্য এনুমারেটর পান

  4. C#-এ StringCollection-এর মাধ্যমে পুনরাবৃত্তি করার জন্য এনুমারেটর পান