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

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

ListDictionary ক্লাস (namespace: System.Collections.Specialized) একটি সিঙ্গলি-লিঙ্কড লিস্ট বেসড ডিকশনারি যা ছোট ডেটা সেটের জন্য অপ্টিমাইজড। এর GetEnumerator() মেথড একটি IDictionaryEnumerator রিটার্ন করে যা কী-ভ্যালু জোড়াগুলোর মাধ্যমে পুনরাবৃত্তি করতে দেয়।

উদাহরণ ১: দুটি ListDictionary এবং এনুমারেটর ব্যবহার

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

public class Demo {
    public static void Main() {
        ListDictionary dict1 = new ListDictionary();
        dict1.Add("A", "বই");
        dict1.Add("B", "ইলেকট্রনিক্স");
        dict1.Add("C", "স্মার্ট ওয়েয়ারেবল");
        dict1.Add("D", "পেট সাপ্লাই");
        dict1.Add("E", "পোশাক");
        dict1.Add("F", "ফুটওয়েয়ার");

        Console.WriteLine("ListDictionary1 উপাদানসমূহ...");
        foreach (DictionaryEntry d in dict1) {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        ListDictionary dict2 = new ListDictionary();
        dict2.Add("1", "এক");
        dict2.Add("2", "দুই");
        dict2.Add("3", "তিন");
        dict2.Add("4", "চার");
        dict2.Add("5", "পাঁচ");
        dict2.Add("6", "ছয়");

        Console.WriteLine("\nListDictionary2 কী-ভ্যালু জোড়া...");
        IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
        while (demoEnum.MoveNext()) {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

আউটপুট

ListDictionary1 উপাদানসমূহ...
A বই
B ইলেকট্রনিক্স
C স্মার্ট ওয়েয়ারেবল
D পেট সাপ্লাই
E পোশাক
F ফুটওয়েয়ার

ListDictionary2 কী-ভ্যালু জোড়া...
Key = 1, Value = এক
Key = 2, Value = দুই
Key = 3, Value = তিন
Key = 4, Value = চার
Key = 5, Value = পাঁচ
Key = 6, Value = ছয়

উদাহরণ ২: একক ListDictionary-র জন্য এনুমারেটর

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

public class Demo {
    public static void Main() {
        ListDictionary dict = new ListDictionary();
        dict.Add("1", "এক");
        dict.Add("2", "দুই");
        dict.Add("3", "তিন");
        dict.Add("4", "চার");
        dict.Add("5", "পাঁচ");
        dict.Add("6", "ছয়");

        Console.WriteLine("ListDictionary কী-ভ্যালু জোড়া...");
        IDictionaryEnumerator demoEnum = dict.GetEnumerator();
        while (demoEnum.MoveNext()) {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

আউটপুট

ListDictionary কী-ভ্যালু জোড়া...
Key = 1, Value = এক
Key = 2, Value = দুই
Key = 3, Value = তিন
Key = 4, Value = চার
Key = 5, Value = পাঁচ
Key = 6, Value = ছয়

আধুনিক ও সহজ পদ্ধতি: foreach লুপ

// পদ্ধতি ১: DictionaryEntry ব্যবহার
foreach (DictionaryEntry entry in dict) {
    Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
}

// পদ্ধতি ২: KeyValuePair (জেনেরিক নন-জেনেরিক মিশ্রণ)
foreach (var entry in dict) {
    // entry টাইপ হবে DictionaryEntry
    Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
}

// পদ্ধতি ৩: শুধু কী বা শুধু ভ্যালু
foreach (var key in dict.Keys) {
    Console.WriteLine("Key: " + key);
}
foreach (var value in dict.Values) {
    Console.WriteLine("Value: " + value);
}

ListDictionary-এর বৈশিষ্ট্য

  • ছোট আকারের ডেটা (সাধারণত ১০টি বা তার কম উপাদান) এর জন্য অপ্টিমাইজড।
  • সিঙ্গলি-লিঙ্কড লিস্ট বাস্তবায়ন।
  • কী-ভ্যালু জোড়া ইনসারশন অর্ডারে সংরক্ষিত থাকে (সর্টেড নয়)।
  • IDictionary ইন্টারফেস বাস্তবায়ন করে।
  • বড় ডেটা সেটের জন্য HybridDictionary বা Hashtable ভালো পারফর্ম করে।

সুপারিশ: নতুন কোডে Dictionary<TKey, TValue> বা SortedDictionary<TKey, TValue> ব্যবহার করুন, যা টাইপ-সেফ এবং বেশি পারফরম্যান্ট।

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

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

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

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