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:

Download  Run Code

 
This method requires .NET framework >= 3.5. Note that the == operator or Enumerable.Equals method checks for reference equality in C#. For example,

Download  Run Code

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.

Download  Run Code

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.

Download  Run Code

That’s all about checking if two Lists are equal in C#.