C#比较两个List集合内容是否相同的几种方法

 更新时间:2025年02月09日 10:23:28   作者:xiaowu080  
本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、排序后比较或HashSet来忽略重复元素,对于自定义类,需要重写Equals和GetHashCode方法,然后使用相应的比较方法

在 C# 中,要比较两个 List<T> 集合的内容是否相同,可以通过以下几种方法:

 一、非自定义类的元素比较

1. 使用 SequenceEqual 方法(顺序和内容都相等)

顺序和内容都相等:使用 SequenceEqual

using System;
using System.Collections.Generic;
using System.Linq;
 
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 = list1.SequenceEqual(list2);
        Console.WriteLine($"Are the lists equal? {areEqual}");
    }
}

2. 自定义比较逻辑(如果顺序不重要)

忽略顺序:可以先对两个列表排序后再使用 SequenceEqual

using System;
using System.Collections.Generic;
using System.Linq;
 
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 = list1.OrderBy(x => x).SequenceEqual(list2.OrderBy(x => x));
        Console.WriteLine($"Are the lists equal (ignoring order)? {areEqual}");
    }
}

3. 使用 Set 比较(忽略重复元素)

忽略重复元素:可以使用 HashSet<T>

using System;
using System.Collections.Generic;
 
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);
        Console.WriteLine($"Are the lists equal (ignoring duplicates)? {areEqual}");
    }
}

二、自定义类的元素比较

如果你想比较自定义对象的集合,比如 List<MyClass>,你需要自定义比较规则。默认情况下,List<T> 的比较是基于对象引用的比较(即两个对象的引用是否相同),而不是根据对象的内容来判断。

为了比较自定义元素,你需要重写 Equals 和 GetHashCode 方法。这样,比较时会依据你定义的规则进行比较。

假设你有一个自定义类 Person,你想根据 Name 和 Age 属性来判断两个 Person 对象是否相同。

重写 Equals 和 GetHashCode

你需要重写 Equals 方法来比较两个对象是否相等,并且重写 GetHashCode,以确保集合操作(如 HashSet 和 Except)正常工作。

1、Equals 方法比较两个 Person 对象的 Name 和 Age 属性。

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

2、GetHashCode 使用 HashCode.Combine 来生成一个基于 Name 和 Age 的哈希值,确保两个内容相同的 Person 对象具有相同的哈希值。

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

1. 顺序和内容都相等

顺序和内容都相等:使用 SequenceEqual

using System;
using System.Collections.Generic;
using System.Linq;
 
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
 
    public override bool Equals(object obj)
    {
        if (obj is Person other)
        {
            return this.Name == other.Name && this.Age == other.Age;
        }
        return false;
    }
 
    public override int GetHashCode()
    {
        return HashCode.Combine(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 = list1.SequenceEqual(list2);
        Console.WriteLine($"顺序和内容都相等: {isSame}");
    }
}

2. 忽略顺序

忽略顺序:先对两个列表排序,然后使用 SequenceEqual

using System;
using System.Collections.Generic;
using System.Linq;
 
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
 
    public override bool Equals(object obj)
    {
        if (obj is Person other)
        {
            return this.Name == other.Name && this.Age == other.Age;
        }
        return false;
    }
 
    public override int GetHashCode()
    {
        return HashCode.Combine(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 = list1.OrderBy(p => p.Name).ThenBy(p => p.Age).SequenceEqual(
            list2.OrderBy(p => p.Name).ThenBy(p => p.Age)
        );
        Console.WriteLine($"忽略顺序: {isSame}");
    }
}

3. 忽略重复元素

忽略重复元素:将列表转换为 HashSet<T>,然后使用 SetEquals 方法进行比较。 

如果你希望忽略重复元素并只关心唯一元素是否相同,可以使用 HashSet<T> 来进行比较。HashSet<T> 会自动去除重复元素,因此可以通过将列表转换为 HashSet<T> 来忽略重复元素。

using System;
using System.Collections.Generic;
 
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
 
    public override bool Equals(object obj)
    {
        if (obj is Person other)
        {
            return this.Name == other.Name && this.Age == other.Age;
        }
        return false;
    }
 
    public override int GetHashCode()
    {
        return HashCode.Combine(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));
        Console.WriteLine($"忽略重复元素: {isSame}");
    }
}

总结

  • 顺序和内容都相等:使用 SequenceEqual
  • 忽略顺序:可以先对两个列表排序后再使用 SequenceEqual
  • 忽略重复元素:可以使用 HashSet<T>

以上就是C#比较两个List集合内容是否相同的几种方法的详细内容,更多关于C#比较两个List内容是否相等的资料请关注脚本之家其它相关文章!

相关文章

  • C# List生成Txt文档并且读取Txt文档封装List

    C# List生成Txt文档并且读取Txt文档封装List

    这篇文章主要介绍了C# List生成Txt文档并且读取Txt文档封装List,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-08-08
  • Unity3d 使用Gizmos画一个圆圈

    Unity3d 使用Gizmos画一个圆圈

    这篇文章主要介绍了Unity3d 使用Gizmos画一个圆圈的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C#设置MDI子窗体只能弹出一个的方法

    C#设置MDI子窗体只能弹出一个的方法

    这篇文章主要介绍了C#设置MDI子窗体只能弹出一个的方法,很实用的技巧,需要的朋友可以参考下
    2014-08-08
  • 使用C#调用百度地图并实现坐标点的设置以及读取示例

    使用C#调用百度地图并实现坐标点的设置以及读取示例

    这篇文章主要介绍了使用C#调用百度地图并实现坐标点的设置以及读取示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • C#加密app.config中连接字符串的方法

    C#加密app.config中连接字符串的方法

    这篇文章主要介绍了C#加密app.config中连接字符串的方法,涉及C#配置文件加密的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • C#实现简单合并word文档的方法

    C#实现简单合并word文档的方法

    这篇文章主要介绍了C#实现简单合并word文档的方法,涉及C#针对word文档的读取、插入、保存等技巧,非常具有实用价值,需要的朋友可以参考下
    2015-09-09
  • C#二维码图片识别代码

    C#二维码图片识别代码

    这篇文章主要为大家详细介绍了C#二维码图片识别代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • C#简单的向量用法实例教程

    C#简单的向量用法实例教程

    这篇文章主要介绍了C#简单的向量用法,需要的朋友可以参考下
    2014-07-07
  • C#中out与ref作用区别示例分析

    C#中out与ref作用区别示例分析

    这篇文章主要为大家介绍了C#中out与ref作用区别示例分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • C# ArrayPool的实现示例

    C# ArrayPool的实现示例

    ArrayPool是.NET中一个高性能数组池,用于减少内存分配和垃圾回收的开销,下面就来介绍一下ArrayPool的具体使用,感兴趣的可以了解一下
    2025-07-07

最新评论