Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ดึง Enumerator เพื่อวนลูปผ่าน ListDictionary ใน C#

ListDictionary ใน namespace System.Collections.Specialized เป็นพจนานุกรมขนาดเล็กที่ใช้ Linked List เก็บข้อมูล ให้เมธอด GetEnumerator() คืนค่า IDictionaryEnumerator สำหรับการวนลูป

ตัวอย่างที่ 1: ใช้ foreach และ IDictionaryEnumerator

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

public class Demo
{
    public static void Main()
    {
        ListDictionary dict1 = new ListDictionary();
        dict1.Add("A", "Books");
        dict1.Add("B", "Electronics");
        dict1.Add("C", "Smart Wearables");
        dict1.Add("D", "Pet Supplies");
        dict1.Add("E", "Clothing");
        dict1.Add("F", "Footwear");

        Console.WriteLine("ListDictionary1 องค์ประกอบ...");
        foreach (DictionaryEntry d in dict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        ListDictionary dict2 = new ListDictionary();
        dict2.Add("1", "One");
        dict2.Add("2", "Two");
        dict2.Add("3", "Three");
        dict2.Add("4", "Four");
        dict2.Add("5", "Five");
        dict2.Add("6", "Six");

        Console.WriteLine("\nListDictionary2 คู่คีย์-ค่า...");
        IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

ผลลัพธ์

ListDictionary1 องค์ประกอบ...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

ListDictionary2 คู่คีย์-ค่า...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six

ตัวอย่างที่ 2: ListDictionary อื่น

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

public class Demo
{
    public static void Main()
    {
        ListDictionary dict = new ListDictionary();
        dict.Add("1", "One");
        dict.Add("2", "Two");
        dict.Add("3", "Three");
        dict.Add("4", "Four");
        dict.Add("5", "Five");
        dict.Add("6", "Six");

        Console.WriteLine("ListDictionary คู่คีย์-ค่า...");
        IDictionaryEnumerator demoEnum = dict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

ผลลัพธ์

ListDictionary คู่คีย์-ค่า...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six

วิธีการวนลูปแบบสมัยใหม่ (แนะนำ)

// 1. ใช้ foreach กับ DictionaryEntry (รองรับใน .NET Framework)
foreach (DictionaryEntry entry in dict)
{
    Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
}

// 2. ใช้ foreach กับ Keys (ต้อง Cast)
foreach (var key in dict.Keys)
{
    Console.WriteLine($"Key: {key}, Value: {dict[key]}");
}

// 3. แปลงเป็น Dictionary แบบ Generic (แนะนำสำหรับโค้ดใหม่)
var genericDict = new Dictionary<string, string>();
foreach (DictionaryEntry entry in dict)
{
    genericDict[(string)entry.Key] = (string)entry.Value;
}
// แล้วใช้ genericDict ได้อย่างเต็มที่

ข้อสังเกตสำคัญ

  • ListDictionary เหมาะสำหรับข้อมูลน้อย (ประมาณ 10 รายการหรือน้อยกว่า) เนื่องเป็น O(n) สำหรับการค้นหา
  • ขนาดใหญ่กว่านี้ ควรใช้ HybridDictionary (สลับอัตโนมัติ) หรือ Dictionary<TKey, TValue>
  • ไม่รองรับคีย์ซ้ำ และไม่เรียงลำดับคีย์
  • คลาสนี้เป็น Legacy แนะนำให้ใช้ Dictionary<TKey, TValue> สำหรับโปรเจกต์ใหม่