SoFunction
Updated on 2025-04-06

Several ways to compare whether the contents of two List collections are the same

In C#, two need to be comparedList<T>Whether the content of the collection is the same can be done in the following ways:

1. Comparison of elements of non-custom classes

1. Use the SequenceEqual method (the order and content are equal)

The order and content are equal:useSequenceEqual

using System;
using ;
using ;
 
class Program
{
    static void Main()
    {
        List<int> list1 = new List<int> { 1, 2, 3, 4 };
        List<int> list2 = new List<int> { 1, 2, 3, 4 };
 
        bool areEqual = (list2);
        ($"Are the lists equal? {areEqual}");
    }
}

2. Customize comparison logic (if the order is not important)

Ignore the order: You can sort the two lists first and then use themSequenceEqual

using System;
using ;
using ;
 
class Program
{
    static void Main()
    {
        List<int> list1 = new List<int> { 1, 2, 3, 4 };
        List<int> list2 = new List<int> { 4, 3, 2, 1 };
 
        bool areEqual = (x => x).SequenceEqual((x => x));
        ($"Are the lists equal (ignoring order)? {areEqual}");
    }
}

3. Use Set to compare (ignor duplicate elements)

Ignore duplicate elements: Can be usedHashSet<T>

using System;
using ;
 
class Program
{
    static void Main()
    {
        List<int> list1 = new List<int> { 1, 2, 3, 4 };
        List<int> list2 = new List<int> { 4, 3, 2, 1 };
 
        bool areEqual = new HashSet<int>(list1).SetEquals(list2);
        ($"Are the lists equal (ignoring duplicates)? {areEqual}");
    }
}

2. Comparison of elements of custom class

If you want to compare collections of custom objects, for exampleList<MyClass>, you need to customize the comparison rules. By default,List<T>The comparison is based on the comparison of object references (that is, whether the references of the two objects are the same), rather than judged based on the content of the object.

In order to compare custom elements, you need to rewriteEqualsandGetHashCodemethod. In this way, the comparison will be based on the rules you define.

Suppose you have a custom classPerson, you want toNameandAgeTo determine two attributesPersonWhether the object is the same.

Rewrite Equals and GetHashCode

You need to rewriteEqualsMethod to compare whether two objects are equal and rewriteGetHashCodeto ensure collection operations (such asHashSetandExcept) works normally.

1、EqualsCompare two methodsPersonThe object'sNameandAgeAttributes.

 public override bool Equals(object obj)
    {
        if (obj is Person other)
        {
            return  ==  &&  == ;
        }
        return false;
    }

2、GetHashCodeuseTo generate a basedNameandAgeThe hash value of   ensures that the two contents are the samePersonObjects have the same hash value.

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

1. The order and content are equal

The order and content are equal:useSequenceEqual

using System;
using ;
using ;
 
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
 
    public override bool Equals(object obj)
    {
        if (obj is Person other)
        {
            return  ==  &amp;&amp;  == ;
        }
        return false;
    }
 
    public override int GetHashCode()
    {
        return (Name, Age);
    }
}
 
class Program
{
    static void Main()
    {
        List&lt;Person&gt; list1 = new List&lt;Person&gt;
        {
            new Person { Name = "Alice", Age = 25 },
            new Person { Name = "Bob", Age = 30 }
        };
        List&lt;Person&gt; list2 = new List&lt;Person&gt;
        {
            new Person { Name = "Alice", Age = 25 },
            new Person { Name = "Bob", Age = 30 }
        };
 
        bool isSame = (list2);
        ($"The order and content are equal: {isSame}");
    }
}

2. Ignore the order

Ignore the order: First sort the two lists, then useSequenceEqual

using System;
using ;
using ;
 
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
 
    public override bool Equals(object obj)
    {
        if (obj is Person other)
        {
            return  ==  &amp;&amp;  == ;
        }
        return false;
    }
 
    public override int GetHashCode()
    {
        return (Name, Age);
    }
}
 
class Program
{
    static void Main()
    {
        List&lt;Person&gt; list1 = new List&lt;Person&gt;
        {
            new Person { Name = "Alice", Age = 25 },
            new Person { Name = "Bob", Age = 30 }
        };
        List&lt;Person&gt; list2 = new List&lt;Person&gt;
        {
            new Person { Name = "Bob", Age = 30 },
            new Person { Name = "Alice", Age = 25 }
        };
 
        bool isSame = (p =&gt; ).ThenBy(p =&gt; ).SequenceEqual(
            (p =&gt; ).ThenBy(p =&gt; )
        );
        ($"Ignore the order: {isSame}");
    }
}

3. Ignore duplicate elements

Ignore duplicate elements: Convert the list to HashSet<T> and use the SetEquals method for comparison.

If you want to ignore duplicate elements and only care about whether the unique elements are the same, you can use HashSet<T> for comparison. HashSet<T> automatically removes duplicate elements, so duplicate elements can be ignored by converting the list to HashSet<T> .

using System;
using ;
 
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
 
    public override bool Equals(object obj)
    {
        if (obj is Person other)
        {
            return  ==  &amp;&amp;  == ;
        }
        return false;
    }
 
    public override int GetHashCode()
    {
        return (Name, Age);
    }
}
 
class Program
{
    static void Main()
    {
        List&lt;Person&gt; list1 = new List&lt;Person&gt;
        {
            new Person { Name = "Alice", Age = 25 },
            new Person { Name = "Alice", Age = 25 },
            new Person { Name = "Bob", Age = 30 }
        };
        List&lt;Person&gt; list2 = new List&lt;Person&gt;
        {
            new Person { Name = "Bob", Age = 30 },
            new Person { Name = "Alice", Age = 25 }
        };
 
        bool isSame = new HashSet&lt;Person&gt;(list1).SetEquals(new HashSet&lt;Person&gt;(list2));
        ($"Ignore duplicate elements: {isSame}");
    }
}

Summarize

  • The order and content are equal:useSequenceEqual
  • Ignore the order: You can sort the two lists first and then use themSequenceEqual
  • Ignore duplicate elements: Can be usedHashSet<T>

The above is the detailed content of several methods for C# to compare whether the contents of two List collections are the same. For more information on whether C# compares whether the contents of two Lists are the same, please pay attention to my other related articles!