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

C#-তে SortedDictionary-এর মাধ্যমে পুনরাবৃত্তি করার জন্য Enumerator পেতে GetEnumerator() ব্যবহার

SortedDictionary<TKey, TValue> ক্লাসের GetEnumerator() মেথড একটি IDictionaryEnumerator রিটার্ন করে যা কী-ভ্যালু জোড়াগুলোর মাধ্যমে ক্রমবদ্ধভাবে পুনরাবৃত্তি করতে দেয়। SortedDictionary স্বয়ংক্রিয়ভাবে কী অনুযায়ী সর্টেড থাকে।

উদাহরণ ১: স্ট্রিং ভ্যালুযুক্ত SortedDictionary

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, "মোবাইল");
        sortedDict.Add(200, "ল্যাপটপ");
        sortedDict.Add(300, "ডেস্কটপ");
        sortedDict.Add(400, "স্পিকার");
        sortedDict.Add(500, "হেডফোন");
        sortedDict.Add(600, "ইয়ারফোন");

        Console.WriteLine("SortedDictionary কী-ভ্যালু জোড়া...");
        
        // IDictionaryEnumerator ব্যবহার করে পুনরাবৃত্তি
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext()) {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

আউটপুট

SortedDictionary কী-ভ্যালু জোড়া...
Key = 100, Value = মোবাইল
Key = 200, Value = ল্যাপটপ
Key = 300, Value = ডেস্কটপ
Key = 400, Value = স্পিকার
Key = 500, Value = হেডফোন
Key = 600, Value = ইয়ারফোন

উদাহরণ ২: ইন্টিজার ভ্যালুযুক্ত SortedDictionary

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

আধুনিক পদ্ধতি: foreach লুপ (সরল ও পঠনযোগ্য)

// C# 5.0+ এ foreach ব্যবহার করে সহজে পুনরাবৃত্তি
foreach (var kvp in sortedDict) {
    Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
}

// অথবা KeyValuePair দিয়ে
foreach (KeyValuePair<int, string> kvp in sortedDict) {
    Console.WriteLine("Key = " + kvp.Key + ", Value = " + kvp.Value);
}

তুলনামূলক নোট

পদ্ধতিবিবরণ
GetEnumerator() + IDictionaryEnumeratorপুরানো প্যাটার্ন, .NET Framework 1.x থেকে। Entry, Key, Value প্রোপার্টি সরবরাহ করে।
foreach + KeyValuePair<TKey,TValue>আধুনিক, টাইপ-সেফ, পঠনযোগ্য এবং প্রেরফার্ড পদ্ধতি।
foreach (var kvp in dict)ইমপ্লিসিট টাইপিং (var) ব্যবহার করে সংক্ষিপ্ত কোড।

টিপ: SortedDictionary কী অনুযায়ী স্বয়ংক্রিয় সর্টেড থাকে, তাই এনুমারেশন সবসময় ক্রমবদ্ধ হয়।

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

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

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

  4. C#-এ ড্রাইভ ফরম্যাট জানা