List<T> ใช้เมธอด Equals(object) จาก object ซึ่งเป็น reference equality คืนค่า true ก็ต่อเมื่อตัวแปรทั้งสองอ้างอิง อ็อบเจ็กต์เดียวกัน ไม่ได้เปรียบเทียบเนื้อหา
ตัวอย่างโค้ดที่ 1: ต่าง Instance (False)
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main(string[] args)
{
List list1 = new List();
list1.Add("One");
list1.Add("Two");
list1.Add("Three");
list1.Add("Four");
list1.Add("Five");
Console.WriteLine("Elements in List1...");
foreach (string res in list1)
{
Console.WriteLine(res);
}
List list2 = new List();
list2.Add("India");
list2.Add("US");
list2.Add("UK");
list2.Add("Canada");
list2.Add("Poland");
list2.Add("Netherlands");
Console.WriteLine("Elements in List2...");
foreach (string res in list2)
{
Console.WriteLine(res);
}
Console.WriteLine("Is List2 equal to List1? = " + list2.Equals(list1));
}
}
ผลลัพธ์
Elements in List1...
One
Two
Three
Four
Five
Elements in List2...
India
US
UK
Canada
Poland
Netherlands
Is List2 equal to List1? = False
ตัวอย่างโค้ดที่ 2: Reference Assignment (True)
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main(string[] args)
{
List list1 = new List();
list1.Add("One");
list1.Add("Two");
list1.Add("Three");
list1.Add("Four");
list1.Add("Five");
Console.WriteLine("Elements in List1...");
foreach (string res in list1)
{
Console.WriteLine(res);
}
List list2 = new List();
list2.Add("India");
list2.Add("US");
list2.Add("UK");
list2.Add("Canada");
list2.Add("Poland");
list2.Add("Netherlands");
Console.WriteLine("Elements in List2...");
foreach (string res in list2)
{
Console.WriteLine(res);
}
List list3 = new List();
list3 = list2; // Reference assignment - ชี้อ็อบเจ็กต์เดียวกัน
Console.WriteLine("Is List3 equals to List2? = " + list3.Equals(list2));
}
}
ผลลัพธ์
Elements in List1...
One
Two
Three
Four
Five
Elements in List2...
India
US
UK
Canada
Poland
Netherlands
Is List3 equals to List2? = True
วิธีเปรียบเทียบเนื้อหา List ให้ถูกต้อง (Value Equality)
1. ใช้ LINQ SequenceEqual (เรียงลำดับเดียวกัน)
using System.Linq;
bool areEqual = list1.SequenceEqual(list2);
2. ใช้ HashSet (ไม่สนใจลำดับ)
bool areEqual = new HashSet<string>(list1).SetEquals(list2);
3. เองเขียน Loop (สำหรับ .NET Framework เก่า)
bool ListsEqual<T>(List<T> a, List<T> b)
{
if (a.Count != b.Count) return false;
for (int i = 0; i < a.Count; i++)
{
if (!Equals(a[i], b[i])) return false;
}
return true;
}
สรุปการเปรียบเทียบ Collection ใน C#
| Type | Equals() | Value Equality Built-in? | วิธีแนะนำ |
|---|---|---|---|
| List<T> | Reference | ไม่มี | SequenceEqual (LINQ) |
| Array | Reference | ไม่มี | SequenceEqual / StructuralComparisons |
| Dictionary | Reference | ไม่มี | Custom / LINQ Except |
| HashSet | Reference | ไม่มี | SetEquals |
| ImmutableList | Value | มี | Equals() |
| ValueTuple | Value | มี | Equals() |
ข้อแนะนำ
- ไม่เคยใช้
list1.Equals(list2)เพื่อเช็คเนื้อหา - จะผิดเสมอหากต่าง instance - ใช้
SequenceEqualสำหรับลำดับสำคัญ,HashSet.SetEqualsสำหรับลำดับไม่สำคัญ - พิจารณา
ImmutableList<T>หากต้องการ value equality built-in