I just came across something pretty weird to me : when you use the Equals() method on a value type (and if this method has not been overriden, of course) you get something very very slow — fields are compared one to one using reflection ! As in :
public struct MyStruct{
int i;
}
(...)
MyStruct s, t;
s.i = 0;
t.i = 1;
if ( s.Equals( t )) /* s.i will be compared to t.i via reflection here. */
(...)
My question : why does the C# compiler do not generate a simple method to compare value types ? Something like (in MyStruct’s definition) :
public override bool Equals( Object o ){
if ( this.i == o.i )
return true;
else
return false;
}
The compiler knows what are the fields of MyStruct at compile time, why does it wait until runtime to enumerate MyStruct fields ?
Very strange to me.
Thanks 🙂
ADDED : Sorry, I just realize that, of course, Equals is not a language keyword but a runtime method… The compiler is completely unaware of this method. So it make sens here to use reflection.
The following is the decompiled ValueType.Equals method from mscorlib:
When possible, a bit-wise comparison will be done (note the CanCompareBits and FastEqualsCheck, both of which are defined as InternalCall. The JIT would presumably inject the appropriate code here. As to why it is so slow, I couldn’t tell you.