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

Kiểm tra hai StringDictionary có bằng nhau trong C#

Giới thiệu

Để kiểm tra hai StringDictionary có bằng nhau, bạn dùng phương thức Equals(object). Tuy nhiên, giống như các collection non-generic khác, Equals mặc định so sánh **tham chiếu** (reference equality), không so sánh nội dung. Hai StringDictionary khác instance nhưng cùng dữ liệu sẽ trả về false.

Ví dụ 1: Ba StringDictionary

using System;
using System.Collections.Specialized;

public class Demo
{
    public static void Main()
    {
        StringDictionary strDict1 = new StringDictionary();
        strDict1.Add("A", "John");
        strDict1.Add("B", "Andy");
        strDict1.Add("C", "Tim");
        strDict1.Add("D", "Ryan");
        strDict1.Add("E", "Kevin");
        strDict1.Add("F", "Katie");
        strDict1.Add("G", "Brad");

        StringDictionary strDict2 = new StringDictionary();
        strDict2.Add("A", "John");
        strDict2.Add("B", "Andy");
        strDict2.Add("C", "Tim");
        strDict2.Add("D", "Ryan");
        strDict2.Add("E", "Kevin");
        strDict2.Add("F", "Katie");
        strDict2.Add("G", "Brad");

        StringDictionary strDict3 = new StringDictionary();
        strDict3 = strDict2; // Gán tham chiếu

        Console.WriteLine("Dictionary2 có bằng Dictionary3 không? = " + strDict2.Equals(strDict3));
        Console.WriteLine("Dictionary1 có bằng Dictionary3 không? = " + strDict1.Equals(strDict3));
    }
}

Kết quả

Dictionary2 có bằng Dictionary3 không? = True
Dictionary1 có bằng Dictionary3 không? = False

Ví dụ 2: Duyệt và so sánh

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

public class Demo
{
    public static void Main()
    {
        StringDictionary strDict1 = new StringDictionary();
        strDict1.Add("A", "John");
        strDict1.Add("B", "Andy");
        strDict1.Add("C", "Tim");
        strDict1.Add("D", "Ryan");
        strDict1.Add("E", "Kevin");
        strDict1.Add("F", "Katie");
        strDict1.Add("G", "Brad");

        Console.WriteLine("Các phần tử của StringDictionary1...");
        foreach (DictionaryEntry d in strDict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        StringDictionary strDict2 = new StringDictionary();
        strDict2.Add("A", "John");
        strDict2.Add("B", "Andy");
        strDict2.Add("C", "Tim");
        strDict2.Add("D", "Ryan");
        strDict2.Add("E", "Kevin");
        strDict2.Add("F", "Katie");
        strDict2.Add("G", "Brad");

        Console.WriteLine("\nCác phần tử của StringDictionary2...");
        foreach (DictionaryEntry d in strDict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("\nDictionary2 có bằng Dictionary1 không? = " + strDict2.Equals(strDict1));
    }
}

Kết quả

Các phần tử của StringDictionary1...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Các phần tử của StringDictionary2...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Dictionary2 có bằng Dictionary1 không? = False

Lưu ý quan trọng: StringDictionary.Equals() so sánh tham chiếu. Dù nội dung y hệt, hai instance khác nhau trả về false. Để so sánh nội dung, hãy duyệt qua các khóa và so sánh giá trị tương ứng. Khóa trong StringDictionary không phân biệt chữ hoa/thường.