Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Kiểm tra hai HybridDictionary bằng nhau trong C#

Phương thức Equals() của HybridDictionary (kế thừa từ object) so sánh **tham chiếu** (reference equality), không so sánh nội dung. Hai biến HybridDictionary chỉ bằng nhau khi trỏ đến **cùng một đối tượng** trong bộ nhớ.

Ví dụ 1: Gán tham chiếu - bằng nhau

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

public class Demo
{
    public static void Main()
    {
        HybridDictionary dict1 = new HybridDictionary();
        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("Các phần tử của HybridDictionary1...");
        foreach (DictionaryEntry d in dict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        HybridDictionary dict2 = new HybridDictionary();
        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("\nCác phần tử của HybridDictionary2...");
        foreach (DictionaryEntry d in dict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        // dict3 tham chiếu đến CÙNG ĐỐI TƯỢNG với dict2
        HybridDictionary dict3 = dict2;

        Console.WriteLine("\nHybridDictionary3 bằng HybridDictionary2? = " + dict3.Equals(dict2));
    }
}

Kết quả

Các phần tử của HybridDictionary1...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

Các phần tử của HybridDictionary2...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
HybridDictionary3 bằng HybridDictionary2? = True

Ví dụ 2: Hai đối tượng khác nhau, nội dung khác - KHÔNG bằng nhau

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

public class Demo
{
    public static void Main()
    {
        HybridDictionary dict1 = new HybridDictionary();
        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("Các phần tử của HybridDictionary1...");
        foreach (DictionaryEntry d in dict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        HybridDictionary dict2 = new HybridDictionary();
        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("\nCác phần tử của HybridDictionary2...");
        foreach (DictionaryEntry d in dict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        // So sánh hai đối tượng KHÁC nhau
        Console.WriteLine("\nHybridDictionary1 bằng HybridDictionary2? = " + dict1.Equals(dict2));
    }
}

Kết quả

Các phần tử của HybridDictionary1...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

Các phần tử của HybridDictionary2...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
HybridDictionary1 bằng HybridDictionary2? = False

Cách so sánh NỘI DUNG của hai HybridDictionary

Vì Equals() so sánh tham chiếu, bạn cần so sánh thủ công:

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

public class Demo
{
    public static bool ContentEquals(HybridDictionary d1, HybridDictionary d2)
    {
        if (d1.Count != d2.Count) return false;
        
        foreach (DictionaryEntry entry in d1)
        {
            if (!d2.Contains(entry.Key)) return false;
            if (!Equals(entry.Value, d2[entry.Key])) return false;
        }
        return true;
    }

    public static void Main()
    {
        HybridDictionary dict1 = new HybridDictionary();
        dict1.Add("A", "Books");
        dict1.Add("B", "Electronics");

        HybridDictionary dict2 = new HybridDictionary();
        dict2.Add("A", "Books");
        dict2.Add("B", "Electronics");

        HybridDictionary dict3 = dict1; // Cùng tham chiếu

        Console.WriteLine("dict1.Equals(dict2) [ref]: " + dict1.Equals(dict2)); // False
        Console.WriteLine("dict1.Equals(dict3) [ref]: " + dict1.Equals(dict3)); // True
        Console.WriteLine("ContentEquals(dict1, dict2): " + ContentEquals(dict1, dict2)); // True
    }
}

So sánh với Dictionary<TKey, TValue> hiện đại

Đặc điểm HybridDictionary Dictionary<TKey,TValue>
Equals() Reference equality Reference equality
So sánh nội dung Thủ công / LINQ Thủ công / LINQ
Type-safe Không Có
Cách so sánh nội dung (Dictionary)
dict1.OrderBy(x => x.Key).SequenceEqual(dict2.OrderBy(x => x.Key))
// Dictionary - so sánh nội dung
var dict1 = new Dictionary<string, string> { { "A", "1" }, { "B", "2" } };
var dict2 = new Dictionary<string, string> { { "B", "2" }, { "A", "1" } };

// Cách 1: LINQ (cần OrderBy vì Dictionary không đảm bảo thứ tự)
bool equal = dict1.OrderBy(kvp => kvp.Key)
                  .SequenceEqual(dict2.OrderBy(kvp => kvp.Key));

// Cách 2: Thư viện (NuGet: DeepEqual, CompareNetObjects...)

Lưu ý quan trọng

  • HybridDictionary đã lỗi thời - dùng Dictionary<TKey, TValue>
  • Equals() không được override - dùng mặc định của object
  • Không có GetHashCode() phù hợp cho dictionary (không dùng làm key trong dictionary khác)
  • Namespace: System.Collections.Specialized
  • Tự động chuyển ListDictionary ↔ Hashtable tại ngưỡng ~9 phần tử