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

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

Giới thiệu

Phương thức Equals(object) của ListDictionary kế thừa từ object, so sánh tham chiếu (reference equality). Hai instance khác nhau luôn trả về false dù nội dung giống nhau.

Ví dụ 1: Ba ListDictionary, dict3 gán từ dict2

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

        ListDictionary dict3 = new ListDictionary();
        dict3 = dict2; // gán tham chiếu
        Console.WriteLine("\nListDictionary3 bằng ListDictionary2? = " + (dict3.Equals(dict2)));
    }
}

Kết quả

Các phần tử ListDictionary1...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Các phần tử ListDictionary2...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
ListDictionary3 bằng ListDictionary2? = True

Ví dụ 2: So sánh hai instance riêng biệt

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

        Console.WriteLine("\nListDictionary1 bằng ListDictionary2? = " + (dict1.Equals(dict2)));
    }
}

Kết quả

Các phần tử ListDictionary1...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Các phần tử ListDictionary2...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
ListDictionary1 bằng ListDictionary2? = False

Cách so sánh nội dung

  • Duyệt thủ công so sánh Count và từng Key/Value.
  • Hoặc dùng LINQ: dict1.Cast<DictionaryEntry>().SequenceEqual(dict2.Cast<DictionaryEntry>()) (cần cùng thứ tự).
  • Generic Dictionary<TKey, TValue> cũng so sánh tham chiếu; dùng SequenceEqual cho nội dung.