Are different beacuse they are different object references.
You need to override Equals() and GetHashCode() for your object, based on the object data, if you want to behave in that way.
Here you have an example about how to do it, and here a blog post about the guidelines overriding the GetHashCode() method. Hope it helps.
class TwoDPoint : System.Object
{
public readonly int x, y;
public TwoDPoint(int x, int y)
{
this.x = x;
this.y = y;
}
public override bool Equals(System.Object obj)
{
if (obj == null) return false;
TwoDPoint p = obj as TwoDPoint;
if (p == null) return false;
// Return true if the fields match
return (x == p.x) && (y == p.y);
}
public override int GetHashCode()
{
return x ^ y;
}
}
As Servy said in his comment, keep in mind that even overriding GetHashCode() method, you won't be able to have a collision-free hash with that type of data (ever), you can only reduce the collision rate. You need to use Equals() to ensure objects with the same hash are really the same
GetHashCodeto be based on the contents, as the answers state, you'll still need to deal with collisions, which are objects that have different values but resolve to the same hash code. You won't be able to have a collision-free hash with that type of data (ever), you can only reduce the collision rate. You need to useEqualsto ensure objects with the same hash are really the same.