C# 如何比较两个List是否相等?

简介

C# 里,比较两个 List 是否相等,需要考虑多个方面,例如列表中的元素顺序、元素本身是否相等。下面介绍几种常见的比较方法:

基本类型比较(元素顺序必须一致)

csharp 复制代码
var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 1, 2, 3 };

bool areEqual = list1.SequenceEqual(list2); // ✅ true

忽略顺序比较

csharp 复制代码
var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 3, 2, 1 };

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

或先分别排完序,再比较:

csharp 复制代码
list3.Sort();
list4.Sort();
Console.WriteLine(list3.SequenceEqual(list4)); // 输出: True

复杂类型(自定义对象列表)

  • 实现 EqualsGetHashCode 方法
csharp 复制代码
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 == person.Name && Age == person.Age;
        }
        return false;
    }

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

使用:

csharp 复制代码
Console.WriteLine(person1.SequenceEqual(person2)); // 输出: True
  • 自定义比较器:
csharp 复制代码
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
csharp 复制代码
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 HashCode.Combine(obj.Name, obj.Age);
        // 还有一种写法:
        // return obj.Name.GetHashCode() ^ obj.Age.GetHashCode();
    }
}

使用方式:

csharp 复制代码
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 = list1.SequenceEqual(list2, new PersonComparer()); // ✅ true

判断是否完全包含对方(不关心顺序)

csharp 复制代码
bool setEqual = list1.Count == list2.Count &&
                !list1.Except(list2).Any() &&
                !list2.Except(list1).Any();

使用 SetEquals(无序、无重复判断)

csharp 复制代码
bool areEqual = new HashSet<int>(list1).SetEquals(list2);

或:

csharp 复制代码
HashSet<int> set1 = new HashSet<int>(list3);
HashSet<int> set2 = new HashSet<int>(list4);

bool isEqual = set1.SetEquals(set2);
Console.WriteLine(isEqual); // 输出: True

比较两个 null 列表

csharp 复制代码
List<int>? list5 = null;
List<int>? list6 = null;
Console.WriteLine(list5 == list6); // 输出: True

比较两个包含null元素的列表

csharp 复制代码
List<string?> list7 = new List<string?> { "a", null };
List<string?> list8 = new List<string?> { "a", null };
Console.WriteLine(list7.SequenceEqual(list8)); // 输出: True

性能优化建议

  • 小规模数据:使用 SequenceEqualHashSet

  • 大规模数据:

    • 先检查列表长度是否相同。
    • 使用并行化处理(如 AsParallel().SequenceEqual())。

总结

场景 方法 是否考虑顺序 是否考虑重复次数
顺序敏感且内容相同 SequenceEqual
顺序不敏感且内容相同 HashSet.SetEquals
顺序不敏感但重复次数相同 排序后使用 SequenceEqual
自定义对象比较 重写 Equals 或使用 IEqualityComparer 可配置 可配置
相关推荐
yi碗汤园3 小时前
【一文了解】八大排序-插入排序、希尔排序
开发语言·算法·unity·c#·1024程序员节
小朩4 小时前
数据结构C语言
数据结构·c#·1024程序员节
CN.LG5 小时前
C# 企业微信机器人消息推送
c#·企业微信·1024程序员节·机器人推送
唐青枫6 小时前
C#.NET ArrayPool 深入解析:高性能内存池的实现与应用
c#·.net
张人玉6 小时前
WPF 核心概念笔记(补充示例)
c#·wpf·1024程序员节·布局控件
foundbug9996 小时前
C# 实现 Modbus TCP 通信
开发语言·tcp/ip·c#
hoiii1877 小时前
C#实现摄像头视频录制与保存
开发语言·c#·音视频
YuanlongWang7 小时前
C# 设计模式——观察者
windows·设计模式·c#
李趣趣16 小时前
数据库字段类型bit容易被忽视的bug
c#·bug
虚行17 小时前
C#OPC客户端通信实操
c#