SoFunction
Updated on 2025-04-17

Common ways to compare whether two lists are equal in C#

Introduction

existC#, compare twoListWhether it is equal requires consideration of multiple aspects, such as the order of elements in the list and whether the elements themselves are equal. Here are some common comparison methods:

Basic type comparison (element order must be consistent)

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 1, 2, 3 };

bool areEqual = (list2); // ✅ true

Ignore the order comparison

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 3, 2, 1 };

bool areEqual = (x => x).SequenceEqual((x => x)); // ✅ true

Or sort them separately first and then compare:

();
();
((list4)); // Output: True

Complex type (custom object list)

  • accomplishEqualsandGetHashCodemethod
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public override bool Equals(object? obj)
    {
        if (obj is Person person)
        {
            return Name ==  && Age == ;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return (Name, Age);
    }
}

use:

((person2)); // Output: True
  • Custom comparator:
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public class PersonComparer : IEqualityComparer&lt;Person&gt;
{
    public bool Equals(Person? x, Person? y)
    {
        return x?.Name == y?.Name &amp;&amp; x?.Age == y?.Age;
    }

    public int GetHashCode(Person obj)
    {
        return (, );
        // There is another way to write it:        // return () ^ ();
    }
}

How to use:

var list1 = new List<Person>
{
    new Person { Name = "Alice", Age = 25 },
    new Person { Name = "Bob", Age = 30 }
};

var list2 = new List<Person>
{
    new Person { Name = "Alice", Age = 25 },
    new Person { Name = "Bob", Age = 30 }
};

bool areEqual = (list2, new PersonComparer()); // ✅ true

Determine whether the other party is completely included (not caring about the order)

bool setEqual =  ==  &&
                !(list2).Any() &&
                !(list1).Any();

Use SetEquals (unordered, no duplicate judgment)

bool areEqual = new HashSet<int>(list1).SetEquals(list2);

or:

HashSet&lt;int&gt; set1 = new HashSet&lt;int&gt;(list3);
HashSet&lt;int&gt; set2 = new HashSet&lt;int&gt;(list4);

bool isEqual = (set2);
(isEqual); // Output: True

Compare two null lists

List&lt;int&gt;? list5 = null;
List&lt;int&gt;? list6 = null;
(list5 == list6); // Output: True

Compare two lists containing null elements

List&lt;string?&gt; list7 = new List&lt;string?&gt; { "a", null };
List&lt;string?&gt; list8 = new List&lt;string?&gt; { "a", null };
((list8)); // Output: True

Performance optimization suggestions

  • Small-scale data: UseSequenceEqualorHashSet

  • Large-scale data:

    • First check whether the list length is the same.
    • Use parallel processing (e.g.AsParallel().SequenceEqual())。

Summarize

Scene method Whether to consider order Whether to consider the number of repetitions
Sensitive order and same content SequenceEqual yes yes
Insensitive order and same content no no
Insensitive order but same number of repetitions Use after sortingSequenceEqual no yes
Custom object comparison RewriteEqualsOr useIEqualityComparer Configurable Configurable

This is the article about the common methods of comparing whether two Lists are equal in C#. For more relevant C# comparison of two Lists equal in C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!