Check if two Lists are equal in C#
This post will discuss how to check if two Lists are equal in C#. Two lists are considered equal if are structurally equal. i.e. they have the same values, in the same order.
1. Using Enumerable.SequenceEqual Method
The standard solution to check for structural equality in C# is using the Enumerable.SequenceEqual method from LINQ. It compares the corresponding elements of both sequences with the custom or default equality comparer. Here’s an example of its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<string> first = new List<string>() { "A", "B", "C" }; List<string> second = new List<string>() { "A", "B", "C" }; bool isEqual = first.SequenceEqual(second); Console.WriteLine(isEqual); // True } } |
This method requires .NET framework >= 3.5. Note that the == operator or Enumerable.Equals method checks for reference equality in C#. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<string> first = new List<string>() { "A", "B", "C" }; List<string> second = new List<string>() { "A", "B", "C" }; bool isEqual = first.Equals(second); Console.WriteLine(isEqual); // False } } |
2. Using Loop
You can even write your own custom routine for checking the equality of two lists. This can be implemented as follows in C# using regular for loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Collections.Generic; public static class Extensions { public static bool isEqual<T>(this List<T> first, List<T> second) { if (first == null && second == null) { return true; } if (first == null || second == null || first.Count != second.Count) { return false; } for (var i = 0; i < first.Count; i++) { var comparer = EqualityComparer<T>.Default; if (!comparer.Equals(first[i], second[i])) { return false; } } return true; } } public class Example { public static void Main() { List<string> first = new List<string>() { "A", "B", "C" }; List<string> second = new List<string>() { "A", "B", "C" }; bool isEqual = first.isEqual(second);; Console.WriteLine(isEqual); // True } } |
3. Using String.Join Method
Finally, you can just compare the string representation of both lists with the == operator. The following solution uses the String.Join method to get the string representation of the specified list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<string> first = new List<string>() { "A", "B", "C" }; List<string> second = new List<string>() { "A", "B", "C" }; bool isEqual = String.Join(",", first) == String.Join(",", second); Console.WriteLine(isEqual); // True } } |
That’s all about checking if two Lists are equal in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)