i have two lists:
List<comparerobj> list_c = new List<comparerobj>();
List<comparerobj> list_b = new List<comparerobj>();
i’m filling lists somehow
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);
}
}
but for any b i’m getting that lc = true. what am i doing wrong?
How your objects are compared? By default .NET framework compares objects by references. For Example if you create following class:
and follwing code
then you will find that these objects are different.
You need to override
Equalsmethod for .NET Framework to treat above objects as equal.