it编程 > 编程语言 > C#

C#中比较两个List是否相等的常见方法

18人参与 2025-04-24 C#

简介

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

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

var list1 = new list<int> { 1, 2, 3 };
var list2 = new list<int> { 1, 2, 3 };

bool areequal = list1.sequenceequal(list2); // ✅ true

忽略顺序比较

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

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

list3.sort();
list4.sort();
console.writeline(list3.sequenceequal(list4)); // 输出: true

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

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);
    }
}

使用:

console.writeline(person1.sequenceequal(person2)); // 输出: true
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 hashcode.combine(obj.name, obj.age);
        // 还有一种写法:
        // return obj.name.gethashcode() ^ obj.age.gethashcode();
    }
}

使用方式:

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

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

bool setequal = list1.count == list2.count &&
                !list1.except(list2).any() &&
                !list2.except(list1).any();

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

bool areequal = new hashset<int>(list1).setequals(list2);

或:

hashset<int> set1 = new hashset<int>(list3);
hashset<int> set2 = new hashset<int>(list4);

bool isequal = set1.setequals(set2);
console.writeline(isequal); // 输出: true

比较两个 null 列表

list<int>? list5 = null;
list<int>? list6 = null;
console.writeline(list5 == list6); // 输出: true

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

list<string?> list7 = new list<string?> { "a", null };
list<string?> list8 = new list<string?> { "a", null };
console.writeline(list7.sequenceequal(list8)); // 输出: true

性能优化建议

总结

场景方法是否考虑顺序是否考虑重复次数
顺序敏感且内容相同sequenceequal
顺序不敏感且内容相同hashset.setequals
顺序不敏感但重复次数相同排序后使用 sequenceequal
自定义对象比较重写 equals 或使用 iequalitycomparer可配置可配置

到此这篇关于c#中比较两个list是否相等的常见方法的文章就介绍到这了,更多相关c#比较两个list相等内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

C#中dictionary如何根据索引值获取Key值

04-24

C#中Thread.CurrentThread的用法小结

04-24

C#中同步和异步回调的实现

04-24

C#中实现CAN通信的使用

04-24

C#之线程同步Mutex类方式

04-24

C# 多项目打包时如何将项目引用转为包依赖(最新推荐)

04-24

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论