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 可配置 可配置
相关推荐
Crazy Struggle15 小时前
.NET 中如何快速实现 List 集合去重?
c#·.net
xb113216 小时前
C#生产者-消费者模式
开发语言·c#
今晚打老虎z16 小时前
解决SQL Server 安装运行时针对宿主机内存不足2GB的场景
sqlserver·c#
Traced back17 小时前
# C# WinForms 数据库清理系统基础知识与避坑指南
开发语言·数据库·c#
我要打打代码19 小时前
关于C#线程 任务
开发语言·数据库·c#
Traced back19 小时前
# C# 基础语法完全指南
开发语言·c#
大黄说说20 小时前
TensorRTSharp 实战指南:用 C# 驱动 GPU,实现毫秒级 AI 推理
开发语言·人工智能·c#
芳草萋萋鹦鹉洲哦20 小时前
后端C#,最好能跨平台,桌面应用框架如何选择?
开发语言·c#
kylezhao201921 小时前
C#中开放 - 封闭原则(**Open-Closed Principle,OCP**)
服务器·c#·开闭原则
百锦再1 天前
《C#上位机开发从门外到门内》2-7:网络通信(TCP/IP、UDP)
tcp/ip·udp·c#·嵌入式·上位机·通信·下位机