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

วิธีตรวจสอบว่า StringCollection สองตัวเท่ากันหรือไม่ใน C#

บทความนี้สาธิตวิธีการตรวจสอบว่า StringCollection สองตัวเท่ากันหรือไม่ โดยใช้ method Equals(object obj)

ตัวอย่างที่ 1: เปรียบเทียบ StringCollection สองตัวที่มีข้อมูลเหมือนกัน

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("StringCollection1 elements...");
        foreach (string res in strCol1)
        {
            Console.WriteLine(res);
        }

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

        Console.WriteLine("StringCollection2 elements...");
        foreach (string res in strCol2)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("Both the String Collections are equal? = " + strCol1.Equals(strCol2));
    }
}

ผลลัพธ์

StringCollection1 elements...
Accessories
Books
Electronics
StringCollection2 elements...
Accessories
Books
Electronics
Both the String Collections are equal? = False

ตัวอย่างที่ 2: เปรียบเทียบอ้างอิงเดียวกัน

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("StringCollection1 elements...");
        foreach (string res in strCol1)
        {
            Console.WriteLine(res);
        }

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

        Console.WriteLine("StringCollection2 elements...");
        foreach (string res in strCol2)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("Both the String Collections are equal? = " + strCol1.Equals(strCol2));

        StringCollection strCol3 = new StringCollection();
        strCol3 = strCol2; // อ้างอิง object เดียวกัน

        Console.WriteLine("Is StringCollection3 equal to StringCollection2? = " + strCol3.Equals(strCol2));
    }
}

ผลลัพธ์

StringCollection1 elements...
Accessories
Books
Electronics
StringCollection2 elements...
Accessories
Books
Electronics
Both the String Collections are equal? = False
Is StringCollection3 equal to StringCollection2? = True

คำอธิบายเพิ่มเติม

  • Equals(object obj) ของ StringCollection ตรวจสอบ reference equality (ชี้ไป object เดียวกัน)
  • strCol3 = strCol2 ทำให้ทั้งสองชี้ไป object เดียวกัน จึงได้ True
  • strCol1 และ strCol2 เป็น object คนละตัว แม้ข้อมูลเหมือนกันหมดก็ได้ False
  • StringCollection ไม่มีการ override Equals สำหรับ value equality (element-by-element)
  • ถ้าต้องการเปรียบเทียบค่า ต้องใช้ LINQ: strCol1.Cast<string>().SequenceEqual(strCol2.Cast<string>())
  • StringCollection เป็น legacy class - แนะนำใช้ List<string> ซึ่งมี behavior เหมือนกัน (reference equality)
  • List<T> ก็ไม่ได้ override Equals สำหรับ value equality เช่นกัน