我有两个列表:
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.