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

การได้ Enumerator วนลูป Hashtable ใน C#

Hashtable มีเมธอด GetEnumerator() คืน IDictionaryEnumerator ซึ่งรองรับ Key และ Value property แยกกัน สะดวกกว่า IEnumerator ธรรมดาที่มีแค่ Current (เป็น DictionaryEntry)

ตัวอย่างโค้ดที่ 1: 10 รายการ พร้อม Enumerator

using System;
using System.Collections;

public class Demo
{
    public static void Main()
    {
        Hashtable hash = new Hashtable(10);
        hash.Add("1", "A");
        hash.Add("2", "B");
        hash.Add("3", "C");
        hash.Add("4", "D");
        hash.Add("5", "E");
        hash.Add("6", "F");
        hash.Add("7", "G");
        hash.Add("8", "H");
        hash.Add("9", "I");
        hash.Add("10", "J");

        Console.WriteLine("Hashtable Key and Value pairs...");
        foreach (DictionaryEntry entry in hash)
        {
            Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
        }

        Console.WriteLine("Is the Hashtable having fixed size? = " + hash.IsFixedSize);
        Console.WriteLine("Count of entries in Hashtable = " + hash.Count);

        Console.WriteLine("\nEnumerator to iterate through the Hashtable...");
        IDictionaryEnumerator demoEnum = hash.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        hash.Clear();
    }
}

ผลลัพธ์

Hashtable Key and Value pairs...
10 and J
1 and A
2 and B
3 and C
4 and D
5 and E
6 and F
7 and G
8 and H
9 and I
Is the Hashtable having fixed size? = False
Count of entries in Hashtable = 10
Enumerator to iterate through the Hashtable...
Key = 10, Value = J
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E
Key = 6, Value = F
Key = 7, Value = G
Key = 8, Value = H
Key = 9, Value = I

ตัวอย่างโค้ดที่ 2: 6 รายการ

using System;
using System.Collections;

public class Demo
{
    public static void Main()
    {
        Hashtable hash = new Hashtable(10);
        hash.Add("One", "A");
        hash.Add("Two", "B");
        hash.Add("Three", "C");
        hash.Add("Four", "D");
        hash.Add("Five", "E");
        hash.Add("Six", "F");

        Console.WriteLine("Hashtable Key and Value pairs...");
        Console.WriteLine("\nEnumerator to iterate through the Hashtable...");
        IDictionaryEnumerator demoEnum = hash.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        hash.Clear();
    }
}

ผลลัพธ์

Hashtable Key and Value pairs...
Enumerator to iterate through the Hashtable...
Key = One, Value = A
Key = Five, Value = E
Key = Three, Value = C
Key = Two, Value = B
Key = Four, Value = D
Key = Six, Value = F

IDictionaryEnumerator vs IEnumerator

คุณสมบัติIEnumeratorIDictionaryEnumerator
Currentobject (DictionaryEntry)object (DictionaryEntry)
Keyไม่มีมี (object)
Valueไม่มีมี (object)
Entryไม่มีมี (DictionaryEntry)
ใช้กับIEnumerableIDictionary

วิธีวนลูป Hashtable ทุกแบบ


// 1. foreach (DictionaryEntry) - แนะนำ
foreach (DictionaryEntry entry in hash)
    Console.WriteLine($"{entry.Key}: {entry.Value}");

// 2. IDictionaryEnumerator - ต้องการ Key/Value แยก
var enum = hash.GetEnumerator();
while (enum.MoveNext())
    Console.WriteLine($"{enum.Key}: {enum.Value}");

// 3. Keys + indexer
foreach (var key in hash.Keys)
    Console.WriteLine($"{key}: {hash[key]}");

// 4. LINQ (Cast)
foreach (var entry in hash.Cast())
    Console.WriteLine($"{entry.Key}: {entry.Value}");

ข้อควรระวัง

  • Hashtable ไม่รักษาลำดับ - การวนลูปไม่ได้ใน insertion order
  • Equals() ของ Hashtable เป็น reference equality
  • ในโค้ดใหม่ ใช้ Dictionary<TKey, TValue> แทน - type-safe, เร็วกว่า, API ทันสมัย
  • Thread-safe: ใช้ Hashtable.Synchronized() หรือ ConcurrentDictionary<TKey, TValue>