If I want to use objects of this class as keys in a Dictionary, what do I need to do? (10.0, 20.0) shouldn’t exist as a key twice.
public class IntPoint
{
public Int64 X { get; set; }
public Int64 Y { get; set; }
public IntPoint(Int64 X, Int64 Y)
{
this.X = X; this.Y = Y;
}
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
}
}
EDIT
public static Dictionary<IntPoint, List<int>> GetPolygonDuplicateIndixes(Polygon p)
{
Dictionary<IntPoint, List<int>> d = new Dictionary<IntPoint, List<int>>();
int i = 0;
foreach(IntPoint point in p)
{
if(!d.ContainsKey(point))
{
d[point] = new List<int>();
}
d[point].Add(i);
i++;
}
...
I’m getting duplicates in d. Why? 22002, 1501 occurs twice in p.
If you look at the Dictionary documentation, you’ll see that if the keys implement
IEquatable, that equality implementation will be used instead.