C# program to check whether two sequences are the same or not

The SequenceEqual method in C# is a LINQ extension method that determines whether two sequences are equal by comparing their elements using the default equality comparer. It returns true if the sequences have the same length and corresponding elements are equal, otherwise it returns false.

Syntax

Following is the syntax for using SequenceEqual method −

bool result = sequence1.SequenceEqual(sequence2);

You can also use a custom equality comparer −

bool result = sequence1.SequenceEqual(sequence2, comparer);

Parameters

  • sequence2 − The sequence to compare with the current sequence.

  • comparer (optional) − An IEqualityComparer to use for comparing elements.

Return Value

Returns true if both sequences have the same length and corresponding elements are equal; otherwise, false.

Using SequenceEqual with String Arrays

Example

using System;
using System.Linq;

class Program {
    static void Main() {
        string[] arr1 = { "This", "is", "it" };
        string[] arr2 = { "My", "work", "report" };
        string[] arr3 = { "This", "is", "it" };
        
        bool res1 = arr1.SequenceEqual(arr2);
        Console.WriteLine("arr1 equals arr2: " + res1);
        
        bool res2 = arr1.SequenceEqual(arr3);
        Console.WriteLine("arr1 equals arr3: " + res2);
    }
}

The output of the above code is −

arr1 equals arr2: False
arr1 equals arr3: True

Using SequenceEqual with Numeric Arrays

Example

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers1 = { 1, 2, 3, 4, 5 };
        int[] numbers2 = { 1, 2, 3, 4, 5 };
        int[] numbers3 = { 1, 2, 3, 4, 6 };
        int[] numbers4 = { 1, 2, 3, 4 };
        
        Console.WriteLine("numbers1 equals numbers2: " + numbers1.SequenceEqual(numbers2));
        Console.WriteLine("numbers1 equals numbers3: " + numbers1.SequenceEqual(numbers3));
        Console.WriteLine("numbers1 equals numbers4: " + numbers1.SequenceEqual(numbers4));
    }
}

The output of the above code is −

numbers1 equals numbers2: True
numbers1 equals numbers3: False
numbers1 equals numbers4: False

Using SequenceEqual with Custom Objects

Example

using System;
using System.Linq;
using System.Collections.Generic;

class Person {
    public string Name { get; set; }
    public int Age { get; set; }
    
    public override bool Equals(object obj) {
        if (obj is Person other) {
            return Name == other.Name && Age == other.Age;
        }
        return false;
    }
    
    public override int GetHashCode() {
        return Name.GetHashCode() ^ Age.GetHashCode();
    }
}

class Program {
    static void Main() {
        List<Person> list1 = new List<Person> {
            new Person { Name = "John", Age = 25 },
            new Person { Name = "Jane", Age = 30 }
        };
        
        List<Person> list2 = new List<Person> {
            new Person { Name = "John", Age = 25 },
            new Person { Name = "Jane", Age = 30 }
        };
        
        List<Person> list3 = new List<Person> {
            new Person { Name = "John", Age = 25 },
            new Person { Name = "Bob", Age = 35 }
        };
        
        Console.WriteLine("list1 equals list2: " + list1.SequenceEqual(list2));
        Console.WriteLine("list1 equals list3: " + list1.SequenceEqual(list3));
    }
}

The output of the above code is −

list1 equals list2: True
list1 equals list3: False

How It Works

The SequenceEqual method performs the following comparison process −

SequenceEqual Comparison Process Check Length Compare Elements Result If lengths differ, return false Compare each element using default or custom comparer True if all elements match Key Points: ? Order matters ? Null handling ? Type compatibility required

Conclusion

The SequenceEqual method provides an efficient way to compare two sequences element by element. It checks both length and content equality, making it perfect for validating that two collections contain the same data in the same order.

Updated on: 2026-03-17T07:04:35+05:30

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements