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

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

C#-এ Dictionary<TKey, TValue> ক্লাসের GetEnumerator() মেথড ব্যবহার করে একটি এনুমারেটর (enumerator) পাওয়া যায় যা ডিকশনারির কী-মান জোড়াগুলোর মাধ্যমে পুনরাবৃত্তি করতে সাহায্য করে। নিচে দুটি উদাহরণ দেওয়া হলো।

উদাহরণ ১

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

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

উদাহরণ ২

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

public class Demo {
    public static void Main() {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        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#-এ OrderedDictionary-এ মানগুলির ICollection পাওয়া (Values প্রপার্টি)

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

  3. সি শার্পে কেস-ইনসেনসিটিভ (Case-insensitive) ডিকশনারি (Dictionary)

  4. C#-এ Dictionary থেকে কীগুলোর লিস্ট পাওয়া