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

การเปรียบเทียบ Hashtable สองอันใน C# (Equals)


เมธอด Hashtable.Equals() ใน C# ใช้สำหรับตรวจสอบว่า Hashtable สองอันเท่ากันหรือไม่ โดยคืนค่า true ก็ต่อเมื่อออบเจ็กต์ทั้งสองเป็นอินสแตนซ์เดียวกัน (reference equality) — ไม่ได้เปรียบเทียบเนื้อหา

ตัวอย่างที่ 1: Hashtable ที่มีค่าเหมือนกันบางส่วน

using System;
using System.Collections;

public class Demo
{
    public static void Main()
    {
        Hashtable hash1 = new Hashtable();
        hash1.Add("1", "Kevin");
        hash1.Add("2", "Steve");
        hash1.Add("3", "Tim");
        hash1.Add("4", "Gary");
        hash1.Add("5", "Kevin");
        hash1.Add("6", "Steve");
        hash1.Add("7", "Tom");
        hash1.Add("8", "Stephen");

        Console.WriteLine("HashSet1...");
        ICollection key = hash1.Keys;
        foreach (string k in key)
        {
            Console.WriteLine(k + ": " + hash1[k]);
        }

        Hashtable hash2 = new Hashtable();
        hash2.Add("1", "Kevin");
        hash2.Add("2", "Steve");
        hash2.Add("3", "John");
        hash2.Add("4", "Tim");

        key = hash2.Keys;
        Console.WriteLine("\nHashSet2...");
        foreach (string k in key)
        {
            Console.WriteLine(k + ": " + hash2[k]);
        }

        Console.WriteLine("\nAre both the Hashtable equal? " + (hash1.Equals(hash2)));
    }
}

ผลลัพธ์

HashSet1...
1: Kevin
2: Steve
3: Tim
4: Gary
5: Kevin
6: Steve
7: Tom
8: Stephen

HashSet2...
1: Kevin
2: Steve
3: John
4: Tim

Are both the Hashtable equal? False

ตัวอย่างที่ 2: Hashtable อื่นที่มีค่าต่างกัน

using System;
using System.Collections;

public class Demo
{
    public static void Main()
    {
        Hashtable hash1 = new Hashtable();
        hash1.Add("1", "Kevin");
        hash1.Add("2", "Steve");
        hash1.Add("3", "John");
        hash1.Add("4", "Tim");

        Console.WriteLine("HashSet1...");
        ICollection key = hash1.Keys;
        foreach (string k in key)
        {
            Console.WriteLine(k + ": " + hash1[k]);
        }

        Hashtable hash2 = new Hashtable();
        hash2.Add("1", "Nathan");
        hash2.Add("2", "Gary");
        hash2.Add("3", "John");
        hash2.Add("4", "Tim");
        hash2.Add("5", "Steve");

        ICollection key2 = hash2.Keys;
        Console.WriteLine("\nHashSet2...");
        foreach (string k in key2)
        {
            Console.WriteLine(k + ": " + hash2[k]);
        }

        Console.WriteLine("\nAre both the Hashtable equal? " + (hash1.Equals(hash2)));
    }
}

ผลลัพธ์

HashSet1...
1: Kevin
2: Steve
3: John
4: Tim

HashSet2...
1: Nathan
2: Gary
3: John
4: Tim
5: Steve

Are both the Hashtable equal? False

ข้อสังเกตสำคัญ — Equals() ไม่เปรียบเทียบเนื้อหา!

  • Hashtable.Equals(object) ใช้ reference equality — คืน true ก็ต่อเมื่ออ้างอิงออบเจ็กต์เดียวกัน
  • แม้ Key/Value จะเหมือนกันทุกคู่ ก็ยังคืน false หากเป็นอินสแตนซ์คนละอัน
  • หากต้องการเปรียบเทียบ เนื้อหา (content equality) ต้องเขียนเอง:

ตัวอย่างการเปรียบเทียบเนื้อหา Hashtable

public static bool ContentEquals(Hashtable h1, Hashtable h2)
{
    if (h1.Count != h2.Count) return false;

    foreach (DictionaryEntry entry in h1)
    {
        if (!h2.ContainsKey(entry.Key)) return false;
        if (!Equals(h2[entry.Key], entry.Value)) return false;
    }
    return true;
}

คำแนะนำ

  • ใช้ Dictionary<TKey, TValue> แทน Hashtable ในโค้ดใหม่ (Generic, type-safe, ประสิทธิภาพดีกว่า)
  • Dictionary ยังไม่มี Equals แบบเนื้อหาในตัว — ต้องใช้ SequenceEqual (LINQ) หรือเขียนเอง
  • Hashtable เป็น non-generic legacy class — ควรหลีกเลี่ยงในโปรเจกต์ใหม่