不一致Equals和GetHash code方法方法、Equals、GetHash、code

2023-09-03 15:55:27 作者:孤久成瘾

看完这个问题Why做" INT"和"为sbyte" GetHash code函数产生不同的值我想进一步挖掘和发现下列行为:?

 为sbyte I = 1;
INT J = 1;
的Object.Equals(I,J)//假(1)
的Object.Equals(J,I)//假(2)
i.Equals(J)//假(3)
j.Equals(ⅰ)//真(4)
我==Ĵ//真(5)
Ĵ== I //真(6)
i.GetHash code()== j.GetHash code()//假(7)
 

(3)和(4)会切断等于应该是对称的要求。的差异 之间的区别(2)和(4)不连贯与 MSDN规范,说

  

如果两个对象不重新present相同的对象的参考,并   既不为null,则调用objA.Equals(objB)并返回结果。   这意味着,如果objA覆盖的Object.Equals(Object)方法,   此覆盖之称。   之间(3)和(5)是指运营商==返回true,但对象是的Equals方面不等于差。   差异(4),(5),(6)和(7)是指两个对象相等的操作者而言==和等于,但它们具有不同的散列codeS。      我很感兴趣,如果任何人都可以解释为什么这种在我看来不一致的行为在相当基本的.NET类型观察到。

解决方案 为什么我们需要重写equals 方法和hashCode 方法

您的问题是,你错过了 i.Equals(J)的隐式转换。它进入到超载 int.Equals(INT)。在这里,我们比较(INT)Ĵ,这是同样的事情。同样的隐式转换会发生在​​ ==

其他的比较上工作的 INT 为sbyte ,它通过定义不同。 j.Equals(我)进入过载 int.Equals(对象),因为参数不是隐转换为为sbyte

等于是对称的他们,但是你的电话code不是。如果你想喝preSS隐式转换为 i.Equals((对象)j)条,它会返回,显示等于的确是对称的。

After reading this question Why do "int" and "sbyte" GetHashCode functions generate different values? I wanted to dig further and found following behavior:

sbyte i = 1;            
int j = 1;
object.Equals(i, j) //false (1)
object.Equals(j, i) //false (2) 
i.Equals(j) //false (3)
j.Equals(i) //true (4)
i == j //true (5)
j == i //true (6)
i.GetHashCode() == j.GetHashCode() //false (7)

Difference between (3) and (4) breaks the requirement that Equals should be symmetric. Difference between (2) and (4) is not coherent with MSDN specification that says:

If the two objects do not represent the same object reference and neither is null, it calls objA.Equals(objB) and returns the result. This means that if objA overrides the Object.Equals(Object) method, this override is called. Difference between (3) and (5) means that operator == returns true, however objects are not equal in terms of Equals. Difference between (4), (5), (6) and (7) means that two objects are equal in terms of operator == and Equals, however they have different hash codes.

I'm very interested if anyone can explain why such in my opinion inconsistent behaviour is observed in rather fundamental .NET types.

解决方案

Your problem is that you missed the implicit conversion in i.Equals(j). It goes to the overload int.Equals(int). Here you're comparing i and (int)j, which are the same thing. The same implicit conversion happens for ==.

The other comparisons work on an int and a sbyte, which by definition are different. j.Equals(i) goes to the overload int.Equals(object), because the argument isn't implicitly convertible to sbyte.

Equals is symmetric for them, but your calling code isn't. If you suppress the implicit conversion with i.Equals((object)j), it'll return false, showing that Equals is indeed symmetric.

 
精彩推荐
图片推荐