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.