Reputation: 1363
When we box two value types(which are different types but compatible to compare the values eg: int and short) and try to call Equals method on that gives false even the values are same.
Case 1:
int a = 5;
short b = 5;
var ob_a = (object) a;
var ob_b = (object) b;
var result = a == b; // true
var result_for_objects = ob_a.Equals(ob_b); // false
On the other hand when the both value types are same the Equals returns the actual value comparison result.
Case 2:
int a = 5;
int b = 5;
var ob_a = (object) a;
var ob_b = (object) b;
var result = a == b; // true
var result_for_objects = ob_a.Equals(ob_b); // true
I compared the both disassembly code for of both case but it was same, I was not able to find any difference.
var result = a == b;
012404DE mov eax,dword ptr [ebp-40h]
012404E1 cmp eax,dword ptr [ebp-44h]
012404E4 sete al
012404E7 movzx eax,al
012404EA mov dword ptr [ebp-50h],eax
var result_for_objects = ob_a.Equals(ob_b);
012404ED mov ecx,dword ptr [ebp-48h]
012404F0 mov edx,dword ptr [ebp-4Ch]
012404F3 mov eax,dword ptr [ecx]
012404F5 mov eax,dword ptr [eax+28h]
012404F8 call dword ptr [eax+4]
012404FB mov dword ptr [ebp-5Ch],eax
012404FE movzx eax,byte ptr [ebp-5Ch]
01240502 mov dword ptr [ebp-54h],eax
Upvotes: 2
Views: 377
Reputation: 1107
The type of the variable is kept in object.
For example:
Console.Write(ob_a.GetType().ToString()); // will give you System.Int32
Console.Write(ob_b.GetType().ToString()); // will give you System.Int16
It gives different hash codes also with method: GetHashCode()
If you will convert short variable to int or the other way, it will be equal... So basically the problem is with different variable types.
Here is the answer for both questions: http://www.ikriv.com/dev/dotnet/ObjectEquality.html
Since call to Equals() is virtual, exact version of the method that will be called by x.Equals(y) is determined by dynamic type of x, that usually is not known at compile time. Note also, that unlike a==b, expression x.Equals(y) is inherently asymmetrical. Only x dictates what version of Equals() will be called. y has absolutely no say in the matter.
Upvotes: 5