T&gt;关于比较列表474上的问题; [英] A question about comparing List<T>
本文介绍了T&gt;关于比较列表474上的问题;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表:
List<comparerobj> list_c = new List<comparerobj>();
List<comparerobj> list_b = new List<comparerobj>();
我正在填充列表莫名其妙
i'm filling lists somehow
然后我试图找到这list_c犯规含有list_b元素:
then i'm trying to find elements in list_b which list_c doesnt contain:
foreach (comparerobj b in list_b)
{
bool lc = !list_c.Contains(b);
if (lc != true)
{
data.Add(b);
}
}
但对于以下b我理解的LC = TRUE。我究竟做错了什么?
but for any b i'm getting that lc = true. what am i doing wrong?
推荐答案
如何你的对象进行比较?默认情况下.NET框架,参考比较的对象。例如,如果您创建下面的类:
How your objects are compared? By default .NET framework compares objects by references. For Example if you create following class:
class A
{
string Name { get; set; }
}
和follwing code
and follwing code
A a1 = new A() { Name = "a" };
A a2 = new A() { Name = "a" };
那么你会发现,这些对象是不同的。
then you will find that these objects are different.
您需要重写等于
方法用于.NET Framework对待上述宗旨,平等的。
You need to override Equals
method for .NET Framework to treat above objects as equal.
这篇关于T&gt;关于比较列表474上的问题;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文