在 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内容是否相等的资料请关注代码网其它相关文章!
发表评论