Introduction
existC#
, compare twoList
Whether 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)
- accomplish
Equals
andGetHashCode
method
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<Person> { public bool Equals(Person? x, Person? y) { return x?.Name == y?.Name && 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<int> set1 = new HashSet<int>(list3); HashSet<int> set2 = new HashSet<int>(list4); bool isEqual = (set2); (isEqual); // Output: True
Compare two null lists
List<int>? list5 = null; List<int>? list6 = null; (list5 == list6); // Output: True
Compare two lists containing null elements
List<string?> list7 = new List<string?> { "a", null }; List<string?> list8 = new List<string?> { "a", null }; ((list8)); // Output: True
Performance optimization suggestions
Small-scale data: Use
SequenceEqual
orHashSet
。-
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 | RewriteEquals Or 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!