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

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

Giới thiệu

Để kiểm tra hai StringCollection có bằng nhau, bạn dùng phương thức Equals(object). Tương tự các collection non-generic, phương thức này so sánh **tham chiếu**, không so sánh nội dung chuỗi.

Ví dụ 1: Hai StringCollection cùng nội dung nhưng khác instance

using System;
using System.Collections.Specialized;

public class Demo
{
    public static void Main()
    {
        StringCollection strCol1 = new StringCollection();
        strCol1.Add("Accessories");
        strCol1.Add("Books");
        strCol1.Add("Electronics");

        Console.WriteLine("Các phần tử của StringCollection1...");
        foreach (string res in strCol1)
            Console.WriteLine(res);

        StringCollection strCol2 = new StringCollection();
        strCol2.Add("Accessories");
        strCol2.Add("Books");
        strCol2.Add("Electronics");

        Console.WriteLine("Các phần tử của StringCollection2...");
        foreach (string res in strCol1) // Bug trong code gốc: duyệt strCol1 thay vì strCol2
            Console.WriteLine(res);

        Console.WriteLine("Cả hai String Collection có bằng nhau không? = " + strCol1.Equals(strCol2));
    }
}

Kết quả

Các phần tử của StringCollection1...
Accessories
Books
Electronics
Các phần tử của StringCollection2...
Accessories
Books
Electronics
Cả hai String Collection có bằng nhau không? = False

Ví dụ 2: Gán tham chiếu

using System;
using System.Collections.Specialized;

public class Demo
{
    public static void Main()
    {
        StringCollection strCol1 = new StringCollection();
        strCol1.Add("Accessories");
        strCol1.Add("Books");
        strCol1.Add("Electronics");

        Console.WriteLine("Các phần tử của StringCollection1...");
        foreach (string res in strCol1)
            Console.WriteLine(res);

        StringCollection strCol2 = new StringCollection();
        strCol2.Add("Accessories");
        strCol2.Add("Books");
        strCol2.Add("Electronics");

        Console.WriteLine("Các phần tử của StringCollection2...");
        foreach (string res in strCol1) // Bug tương tự
            Console.WriteLine(res);

        Console.WriteLine("Cả hai String Collection có bằng nhau không? = " + strCol1.Equals(strCol2));

        StringCollection strCol3 = new StringCollection();
        strCol3 = strCol2; // Gán tham chiếu

        Console.WriteLine("StringCollection3 có bằng StringCollection2 không? = " + strCol3.Equals(strCol2));
    }
}

Kết quả

Các phần tử của StringCollection1...
Accessories
Books
Electronics
Các phần tử của StringCollection2...
Accessories
Books
Electronics
Cả hai String Collection có bằng nhau không? = False
StringCollection3 có bằng StringCollection2 không? = True

Lưu ý quan trọng: StringCollection.Equals() so sánh tham chiếu. Hai collection khác instance nhưng cùng chuỗi, cùng thứ tự vẫn trả về false. Code ví dụ gốc có bug: vòng lặp in StringCollection2 lại duyệt strCol1. Để so sánh nội dung, hãy tự viết code duyệt và so sánh từng phần tử.