0

I have a simple custom class Point:

    public class Point: IEqualityComparer<Point>
{
    public double X;
    public double Y;
    public double Z;
    private double[] startPointCoords;

    public Point()
    {
    }

    public Point(double[] pointArray)
    {
        this.startPointCoords = pointArray;
        X = pointArray[0];
        Y = pointArray[1];
        Z = pointArray[2];
    }

    public bool Equals(Point x, Point y)
    {
        if(x.X == y.X && x.Y == y.Y && x.Z == y.Z)
        {
            return true;
        }
        return false;
    }

    public int GetHashCode(Point obj)
    {
        string xString = X.ToString().Replace(".", "");
        string yString = Y.ToString().Replace(".", "");
        string zString = Z.ToString().Replace(".", "");
        int xInt = Convert.ToInt32(xString);
        int yInt = Convert.ToInt32(yString);
        int zInt = Convert.ToInt32(zString);
        return xInt - yInt + zInt;
    }
}

I am using this class in a Dictionary. I am checking for if the point instance has been added to the dictionary using:

            if (!pointsToProcess.ContainsKey(startPoint))
            {
                pointsToProcess.Add(startPoint, startPoint);
            }

I am debugging my code to make sure Equals is working correctly. My break point I have set in Point.Equals is never hit. I set a break point in Point.GetHashCode and it is never hit either. It seems like they are not being used.

I know that there are classes called Point in .Net. I am absolutely sure that all the Point that I have in my code is from my custom namespace.

Why would my Point.Equals and Point.GetHashCode not be reached when setting a break point?

Eric Snyder
  • 1,816
  • 3
  • 22
  • 46
  • 1
    `IEqualityComparer` should be implemented on classes dedicated to comparing. Seems like you're wanting to implement `IEquatable` instead. – Parrish Husband Apr 09 '20 at 18:08
  • @ParrishHusband - I was optimistic about your answer. I removed the current Equals and GetHashCode, added IEquatable as suggested, implimented Equals and my breakpoint in the new Equals still did not get hit. – Eric Snyder Apr 09 '20 at 18:13
  • It's possible this is being stepped through in the debugger. Have you tested adding a debug console output in the `Equals` method. It would show if the method is being run, but not hitting the breakpoint. – Parrish Husband Apr 09 '20 at 18:16
  • @ParrishHusband - Good idea. The Equals method is definitely NOT being hit. – Eric Snyder Apr 09 '20 at 18:19

1 Answers1

1

The Equals(a, b) method is not hit by IEquatable, so you'll need to tailor it to suit the interface.

Try this one:

public class Point : IEquatable<Point>
{
    public double X;
    public double Y;
    public double Z;
    private double[] startPointCoords;

    public Point()
    {
    }

    public Point(double[] pointArray)
    {
        this.startPointCoords = pointArray;
        X = pointArray[0];
        Y = pointArray[1];
        Z = pointArray[2];
    }

    public override bool Equals(object obj) => Equals(obj as Point);

    public bool Equals(Point other)
    {
        if (other is null)
            return false;

        if (ReferenceEquals(this, other))
            return true;

        return this.X == other.X &&
               this.Y == other.Y &&
               this.Z == other.Z;
    }

    public override int GetHashCode()
    {
        string xString = X.ToString().Replace(".", "");
        string yString = Y.ToString().Replace(".", "");
        string zString = Z.ToString().Replace(".", "");
        int xInt = Convert.ToInt32(xString);
        int yInt = Convert.ToInt32(yString);
        int zInt = Convert.ToInt32(zString);
        return xInt - yInt + zInt;
    }

}

Also there are a lot of ways to implement hashcodes in C# for custom objects. While not perfect, one simple way would be using anonymous object hashing:

public override int GetHashCode()
{
    return new { X, Y, Z }.GetHashCode();
}
Parrish Husband
  • 3,148
  • 18
  • 40