LINQ SequenceEqual

Summary: in this tutorial, you will learn how to use LINQ SequenceEqual() method to compare two sequences.

Introduction to the LINQ SequenceEqual method

The Sequence() method allows you to compare two sequences and return true if they contain the same elements in the same order.

Here’s the syntax of the SequenceEqual() method:

bool SequenceEqual<TSource> (
   this IEnumerable<TSource> first, 
   IEnumerable<TSource> second
);Code language: C# (cs)

In this syntax, the first and second parameters are sequences with the type IEnumerable<T> that you want to compare. By default, the SequenceEqual() method compares the elements in the sequences by their references if they are objects, or by their value if they are primitive types.

If you want to override the default comparer, you can implement your own IEqualityComparer<T> and pass it to the SequenceEqual() method:

bool SequenceEqual<TSource> (
   this IEnumerable<TSource> first, 
   IEnumerable<TSource> second,
   IEqualityComparer<TSource>? comparer
);Code language: C# (cs)

LINQ SequenceEqual() method examples

Let’s explore some examples of using the SequenceEqual() method.

1) Using SequenceEqual() to compare two lists of integers

The following example uses the SequenceEqual() method to compare two lists of integers. It returns true because both lists have the same elements in the same order:

using static System.Console;


var num1 = new List<int> { 1, 2, 3 };
var num2 = new List<int> { 1, 2, 3 };

var result = num1.SequenceEqual(num2);
WriteLine(result); // TrueCode language: C# (cs)

Output:

TrueCode language: C# (cs)

The following example returns false even though both lists contain the same elements, their elements are not in the same order:

using static System.Console;


var num1 = new List<int> { 1, 2, 3 };
var num2 = new List<int> { 1, 3, 2 };

var result = num1.SequenceEqual(num2);
WriteLine(result); // FalseCode language: C# (cs)

In this example, the SequenceEqual() method returns false once it finds that the second element of the first list is not equal to the second element of the second list.

2) Using SequenceEqual() to compare two lists of objects

Suppose, you have a Person class with two properties SSN and Name:

public class Person
{
    public required string SSN
    {
        get; set;
    }
    public required string Name
    {
        get; set;
    }
}Code language: C# (cs)

The following program uses the SequenceEqual() method to compare two lists of the Person objects:

using static System.Console;


var members1 = new List<Person>
{
    new Person() { SSN="111222333", Name="John Doe" },
    new Person() { SSN="111222334", Name="Jane Doe" }
};

var members2 = new List<Person>
{
    new Person() { SSN="111222333", Name="John Doe" },
    new Person() { SSN="111222334", Name="Jane Doe" }
};

var isEqual = members1.SequenceEqual(members2);
WriteLine($"isEqual: {isEqual}"); // FalseCode language: C# (cs)

Output:

FalseCode language: C# (cs)

It returns false even though they seem to have the same elements in the same order. The SequenceEqual() method compares the Person object by reference.

To override the default equality comparer, you can implement a PersonComparer. Suppose two Person objects are the same if they have the same social security number (SSN):

public class PersonComparer : IEqualityComparer<Person>
{
    public bool Equals(Person? x, Person? y)
    {
        // Check for null values
        if (x == null || y == null)
            return false;

        // Check if the two Person objects are the same reference
        if (ReferenceEquals(x, y))
            return true;

        // Compare the SSN of the two Person objects
        // to determine if they're the same
        return x.SSN == y.SSN;
    }

    public int GetHashCode(Person? obj)
    {
        if (obj == null || obj.SSN == null)
            return 0;

        // Use the SSN of the Person object
        // as the hash code
        return obj.SSN.GetHashCode();
    }
}Code language: C# (cs)

The following Program uses the SequenceEqual() method with the PersonComparer. It returns true because the lists contain the same elements in the same order:

using static System.Console;


var members1 = new List<Person>
{
    new Person() { SSN="111222333", Name="John Doe" },
    new Person() { SSN="111222334", Name="Jane Doe" }
};

var members2 = new List<Person>
{
    new Person() { SSN="111222333", Name="John Doe" },
    new Person() { SSN="111222334", Name="Jane Doe" }
};


var isEqual = members1.SequenceEqual(
    members2,
    new PersonComparer()
);

WriteLine($"isEqual: {isEqual}"); // TrueCode language: C# (cs)

Output:

TrueCode language: C# (cs)

Summary

  • Use the SequenceEqual() method to compare two sequences. It returns true if they contain the same elements in the same order.
  • Use a custom EqualityComparer to override the default equality comparer.
Was this tutorial helpful ?