Try this:
[C#]
using System;
namespace Samples
{
public class Point
{
private readonly int _x;
private readonly int _y;
public Point(int x, int y)
{
_x = x;
_y = y;
}
public int X
{
get { return _x; }
}
public int Y
{
get { return _y; }
}
public override int GetHashCode()
{
return _x ^ _y;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, this))
return true;
Point point = obj as Point;
// Either cast point to object or use ReferenceEquals
// to avoid a call to == operator overload
if (ReferenceEquals(point, null))
return false;
return point._x == _x && point._y == _y;
}
public static bool operator ==(Point p1, Point p2)
{
// need to cast to object (that is what ReferenceEquals does)
// to avoid recursion
return !ReferenceEquals(p1, null) && !ReferenceEquals(p2, null) &&
p1._x == p2._x && p1._y == p2._y;
}
public static bool operator !=(Point p1, Point p2)
{
return !(p1 == p2);
}
}
}