How to see common elements between two arrays of objects
4 answers
You need to override the Equals method of the object. You can find some guidelines on the Microsoft website.
I have provided an example below:
public override bool Equals(System.Object obj)
{
if (obj != null && obj is MyClass)
{
MyClass obj2 = (MyClass)obj;
return (obj2.ID == this.ID);
}
}
If you don't override this method, any sort / intersect / compare will compare objects based on their reference; therefore, if two objects refer to the same memory spot, they are considered "equal".
0
a source to share