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 rewriteEquals
andGetHashCode
method. In this way, the comparison will be based on the rules you define.
Suppose you have a custom classPerson
, you want toName
andAge
To determine two attributesPerson
Whether the object is the same.
Rewrite Equals and GetHashCode
You need to rewriteEquals
Method to compare whether two objects are equal and rewriteGetHashCode
to ensure collection operations (such asHashSet
andExcept
) works normally.
1、Equals
Compare two methodsPerson
The object'sName
andAge
Attributes.
public override bool Equals(object obj) { if (obj is Person other) { return == && == ; } return false; }
2、GetHashCode
useTo generate a based
Name
andAge
The hash value of ensures that the two contents are the samePerson
Objects 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 == && == ; } return false; } public override int GetHashCode() { return (Name, Age); } } class Program { static void Main() { List<Person> list1 = new List<Person> { new Person { Name = "Alice", Age = 25 }, new Person { Name = "Bob", Age = 30 } }; List<Person> list2 = new List<Person> { 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 == && == ; } return false; } public override int GetHashCode() { return (Name, Age); } } class Program { static void Main() { List<Person> list1 = new List<Person> { new Person { Name = "Alice", Age = 25 }, new Person { Name = "Bob", Age = 30 } }; List<Person> list2 = new List<Person> { new Person { Name = "Bob", Age = 30 }, new Person { Name = "Alice", Age = 25 } }; bool isSame = (p => ).ThenBy(p => ).SequenceEqual( (p => ).ThenBy(p => ) ); ($"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 == && == ; } return false; } public override int GetHashCode() { return (Name, Age); } } class Program { static void Main() { List<Person> list1 = new List<Person> { new Person { Name = "Alice", Age = 25 }, new Person { Name = "Alice", Age = 25 }, new Person { Name = "Bob", Age = 30 } }; List<Person> list2 = new List<Person> { new Person { Name = "Bob", Age = 30 }, new Person { Name = "Alice", Age = 25 } }; bool isSame = new HashSet<Person>(list1).SetEquals(new HashSet<Person>(list2)); ($"Ignore duplicate elements: {isSame}"); } }
Summarize
-
The order and content are equal:use
SequenceEqual
。 -
Ignore the order: You can sort the two lists first and then use them
SequenceEqual
。 -
Ignore duplicate elements: Can be used
HashSet<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!