Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if two List objects are equal in C#
In C#, there are several ways to check if two List<T> objects are equal. The default Equals() method checks for reference equality, not content equality. For comparing list contents, you need different approaches depending on your specific requirements.
Using Equals() for Reference Equality
The Equals() method on List objects checks if both variables reference the same object in memory −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
List<string> list1 = new List<string> { "One", "Two", "Three" };
List<string> list2 = new List<string> { "One", "Two", "Three" };
List<string> list3 = list1;
Console.WriteLine("list1.Equals(list2): " + list1.Equals(list2));
Console.WriteLine("list1.Equals(list3): " + list1.Equals(list3));
}
}
The output of the above code is −
list1.Equals(list2): False list1.Equals(list3): True
Using LINQ SequenceEqual() for Content Equality
To compare the actual contents of two lists, use the SequenceEqual() method from LINQ −
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main(string[] args) {
List<string> list1 = new List<string> { "Apple", "Banana", "Cherry" };
List<string> list2 = new List<string> { "Apple", "Banana", "Cherry" };
List<string> list3 = new List<string> { "Cherry", "Apple", "Banana" };
Console.WriteLine("Elements in List1: " + string.Join(", ", list1));
Console.WriteLine("Elements in List2: " + string.Join(", ", list2));
Console.WriteLine("Elements in List3: " + string.Join(", ", list3));
Console.WriteLine();
Console.WriteLine("list1.SequenceEqual(list2): " + list1.SequenceEqual(list2));
Console.WriteLine("list1.SequenceEqual(list3): " + list1.SequenceEqual(list3));
}
}
The output of the above code is −
Elements in List1: Apple, Banana, Cherry Elements in List2: Apple, Banana, Cherry Elements in List3: Cherry, Apple, Banana list1.SequenceEqual(list2): True list1.SequenceEqual(list3): False
Custom Method for Element Comparison
You can create a custom method to compare lists with specific logic −
using System;
using System.Collections.Generic;
public class Demo {
public static bool AreListsEqual<T>(List<T> list1, List<T> list2) {
if (list1.Count != list2.Count) return false;
for (int i = 0; i < list1.Count; i++) {
if (!list1[i].Equals(list2[i])) return false;
}
return true;
}
public static void Main(string[] args) {
List<int> numbers1 = new List<int> { 1, 2, 3, 4, 5 };
List<int> numbers2 = new List<int> { 1, 2, 3, 4, 5 };
List<int> numbers3 = new List<int> { 1, 2, 3, 4, 6 };
Console.WriteLine("numbers1: " + string.Join(", ", numbers1));
Console.WriteLine("numbers2: " + string.Join(", ", numbers2));
Console.WriteLine("numbers3: " + string.Join(", ", numbers3));
Console.WriteLine();
Console.WriteLine("AreListsEqual(numbers1, numbers2): " + AreListsEqual(numbers1, numbers2));
Console.WriteLine("AreListsEqual(numbers1, numbers3): " + AreListsEqual(numbers1, numbers3));
}
}
The output of the above code is −
numbers1: 1, 2, 3, 4, 5 numbers2: 1, 2, 3, 4, 5 numbers3: 1, 2, 3, 4, 6 AreListsEqual(numbers1, numbers2): True AreListsEqual(numbers1, numbers3): False
Comparison of Methods
| Method | Type of Equality | Order Matters | Performance |
|---|---|---|---|
| Equals() | Reference equality | N/A | O(1) |
| SequenceEqual() | Content equality | Yes | O(n) |
| Custom method | Content equality | Yes | O(n) |
Conclusion
To check if two List objects are equal in C#, use Equals() for reference equality or SequenceEqual() for content equality. The SequenceEqual() method is the most common approach when you need to compare the actual elements and their order in both lists.
