Trong C#, LinkedList<T> không override phương thức Equals(), do đó nó kế thừa từ object.Equals() - so sánh **tham chiếu** (reference equality). Hai biến LinkedList chỉ bằng nhau khi trỏ đến **cùng một đối tượng** trong bộ nhớ.
Ví dụ 1: Gán tham chiếu - bằng nhau
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main(string[] args)
{
LinkedList<string> list1 = new LinkedList<string>();
list1.AddLast("One");
list1.AddLast("Two");
list1.AddLast("Three");
list1.AddLast("Four");
list1.AddLast("Five");
Console.WriteLine("Các phần tử trong LinkedList1...");
foreach (string res in list1)
{
Console.WriteLine(res);
}
LinkedList<string> list2 = new LinkedList<string>();
list2.AddLast("India");
list2.AddLast("US");
list2.AddLast("UK");
list2.AddLast("Canada");
list2.AddLast("Poland");
list2.AddLast("Netherlands");
Console.WriteLine("\nCác phần tử trong LinkedList2...");
foreach (string res in list2)
{
Console.WriteLine(res);
}
// list3 tham chiếu đến CÙNG ĐỐI TƯỢNG với list2
LinkedList<string> list3 = list2;
Console.WriteLine("\nLinkedList3 bằng LinkedList2? = " + list3.Equals(list2));
}
}
Kết quả
Các phần tử trong LinkedList1... One Two Three Four Five Các phần tử trong LinkedList2... India US UK Canada Poland Netherlands LinkedList3 bằng LinkedList2? = True
Ví dụ 2: Hai LinkedList nội dung giống nhau nhưng đối tượng khác - KHÔNG bằng nhau
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main(string[] args)
{
LinkedList<string> list1 = new LinkedList<string>();
list1.AddLast("One");
list1.AddLast("Two");
list1.AddLast("Three");
list1.AddLast("Four");
list1.AddLast("Five");
Console.WriteLine("Các phần tử trong LinkedList1...");
foreach (string res in list1)
{
Console.WriteLine(res);
}
LinkedList<string> list2 = new LinkedList<string>();
list2.AddLast("India");
list2.AddLast("US");
list2.AddLast("UK");
list2.AddLast("Canada");
list2.AddLast("Poland");
list2.AddLast("Netherlands");
Console.WriteLine("\nCác phần tử trong LinkedList2...");
foreach (string res in list2)
{
Console.WriteLine(res);
}
// So sánh hai đối tượng KHÁC nhau (dù nội dung có thể giống nhau)
Console.WriteLine("\nLinkedList2 bằng LinkedList1? = " + list2.Equals(list1));
}
}
Kết quả
Các phần tử trong LinkedList1... One Two Three Four Five Các phần tử trong LinkedList2... India US UK Canada Poland Netherlands LinkedList2 bằng LinkedList1? = False
Cách so sánh NỘI DUNG của hai LinkedList
Vì Equals() so sánh tham chiếu, bạn cần so sánh thủ công hoặc dùng LINQ:
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo
{
public static void Main()
{
LinkedList<int> list1 = new LinkedList<int>(new[] { 1, 2, 3 });
LinkedList<int> list2 = new LinkedList<int>(new[] { 1, 2, 3 });
LinkedList<int> list3 = new LinkedList<int>(new[] { 1, 2, 4 });
// Cách 1: SequenceEqual (LINQ) - khuyến nghị
bool equal12 = list1.SequenceEqual(list2); // True
bool equal13 = list1.SequenceEqual(list3); // False
Console.WriteLine("list1 == list2 (SequenceEqual): " + equal12);
Console.WriteLine("list1 == list3 (SequenceEqual): " + equal13);
// Cách 2: So sánh thủ công
bool ManualEquals(LinkedList<int> a, LinkedList<int> b)
{
if (a.Count != b.Count) return false;
var nodeA = a.First;
var nodeB = b.First;
while (nodeA != null)
{
if (!EqualityComparer<int>.Default.Equals(nodeA.Value, nodeB.Value))
return false;
nodeA = nodeA.Next;
nodeB = nodeB.Next;
}
return true;
}
Console.WriteLine("Manual equals list1 vs list2: " + ManualEquals(list1, list2));
}
}
So sánh các Collection Type
| Collection | Equals() behavior | So sánh nội dung |
|---|---|---|
| LinkedList<T> | Reference equality | SequenceEqual() (LINQ) |
| List<T> | Reference equality | SequenceEqual() |
| Array | Reference equality | SequenceEqual() hoặc StructuralComparisons.StructuralEqualityComparer |
| HashSet<T> | Value equality (SetEquals) | SetEquals() |
| Dictionary<TKey,TValue> | Reference equality | Custom hoặc LINQ |
| Tuple / ValueTuple | Value equality | Built-in Equals / == |
Lưu ý
LinkedList<T>trongSystem.Collections.Generic- Không override
Equals,GetHashCode- dùng mặc định của object - Dùng
SequenceEqual()(cầnusing System.Linq) để so sánh nội dung SequenceEqualso sánh theo thứ tự và số lượng phần tử- Để so sánh không quan tâm thứ tự:
set1.SetEquals(set2)(chỉ HashSet)