我是C#的新手。也许我没有正确实现IEquatable,因为应该被视为相同类型的对象不是。

班级:

class CompPoint : IComparable {
        public int X;
        public int Y;

        public CompPoint(int X, int Y) {
            this.X = X;
            this.Y = Y;
        }

   public override bool Equals(Object o) {
        if (!(o is CompPoint)) {
            throw new ArgumentException(String.Format("argument is not a CompPoint (%s given)", o));
        }
        CompPoint cp = (CompPoint)o;

        return this.X == cp.X && this.Y == cp.Y;
    }

    public override int GetHashCode() {
        int hash = base.GetHashCode(); // this is a problem. replace with a constant?
        hash = (hash * 73) + this.X.GetHashCode();
        hash = (hash * 73) + this.Y.GetHashCode();
        return hash;
    }
}


CompPoint的更多内容足以证明它是一类。)

然后,此测试失败:

    [TestMethod()]
    public void compPointTest() {
        Assert.AreEqual(new CompPoint(0, 0), new CompPoint(0, 0));
    }


我有什么误会? Assert.AreEqual()使用引用相等吗?我在Equals()中的CompPoint函数是否搞砸了?

此功能也将失败:

    public void EqualsTest() {
        Assert.IsTrue(new CompPoint(1, 1).Equals(new CompPoint(1, 1)));
    }


这样做的原因是,我正在使用Dictionary,并且无法按我希望的方式工作:

    [TestMethod()]
    public void dictCompPointTest() {
        IDictionary<CompPoint, int> dict = new Dictionary<CompPoint, int>();
        dict[new CompPoint(0, 0)] = 4;
        dict[new CompPoint(0, 0)] = 24;
        dict[new CompPoint(0, 0)] = 31;

        Assert.AreEqual(31, dict[new CompPoint(0, 0)]);
        Assert.AreEqual(1, dict.Count);
    }


测试失败,并显示以下消息:


  测试方法
  ShipAILabTest.BoardUtilsTest.dictCompPointTest
  抛出异常:
  System.Collections.Generic.KeyNotFoundException:
  给定的密​​钥不存在于
  字典。


这个测试概括了我的期望。我希望由于每次键都是相同的,因此该值将被覆盖。 Dictionary用于测试相等性的是什么?

更新:按照托马斯的建议,我添加了一个相等函数,现在CompPoint比较测试有效,而dictCompPointTest有效。

    public override bool Equals(Object o) {
        if (!(o is CompPoint)) {
            throw new ArgumentException(String.Format("argument is not a CompPoint (%s given)", o));
        }
        CompPoint cp = (CompPoint)o;

        return this.X == cp.X && this.Y == cp.Y;
    }


神秘地,此测试仍然失败:

   [TestMethod()]
    public void dictCPTest2() {
        IDictionary<CompPoint, int> dict = new Dictionary<CompPoint, int>();
        dict[new CompPoint(2, 2)] = 2;
        dict[new CompPoint(2, 2)] = 2;

        Assert.AreEqual(1, dict.Count);
    }


当键为new CompPoint(4, 1)时,测试也会失败,但是当键为new CompPoint(0, 1)时,测试不会失败。为什么这会为某些价值而不是其他价值努力?

更神秘的是:哈希码功能似乎工作得很差。该测试失败:

    [TestMethod()]
    public void hashCodeTest() {
                int x = 0;
                int y = 0;
                Assert.AreEqual(new CompPoint(x, y).GetHashCode(), new CompPoint(x, y).GetHashCode());
    }


哈希码功能在上面列出。这里有什么问题?这两个CompPoint对象不应该具有相同的哈希码吗?也许我打base.getHashCode()的电话有问题吗?

最佳答案

如果要覆盖Equals,则还应该覆盖GetHashCode,因为这是字典在第一个实例中将用来确定两个键是否匹配的内容。 (被认为相等的任何两个相同类型的对象应从GetHashCode返回相同的值。)

public class CompPoint : IEquatable<CompPoint>
{
    // ...

    public override bool Equals(object obj)    // object
    {
        return this.Equals(obj as ComPoint);
    }

    public bool Equals(CompPoint other)    // IEquatable<ComPoint>
    {
        return !object.ReferenceEquals(other, null)
            && this.X.Equals(other.X)
            && this.Y.Equals(other.Y);
    }

    public override int GetHashCode()    // object
    {
        int hash = 5419;
        hash = (hash * 73) + this.X.GetHashCode();
        hash = (hash * 73) + this.Y.GetHashCode();
        return hash;
    }
}

10-08 19:17