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)
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.
I'm very interested if anyone can explain why such in my opinion inconsistent behaviour is observed in rather fundamental .NET types.
You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object. hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.
Java hashCode() An object hash code value can change in multiple executions of the same application. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.
A hash code is a numeric value which is used to insert and identify an object in a hash-based collection. The GetHashCode method provides this hash code for algorithms that need quick checks of object equality.
We can override the equals method in our class to check whether two objects have same data or not.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With