When I need to define my own Equals method usually I only override Equals(Object) in my class.
However MSDN says:
It is also recommended that in addition to implementing Equals(object), any class also implement Equals(type) for their own type, to enhance performance.
But how could it enhance performance? I can only call one of these two method, don’t I?
No boxing for structs, potential for direct call for all types (instead of virtual call).
If you don’t provide explicit method that takes MyStruct than MyStruct will have to be boxed for every calls to
myFirst.Equals(myOther). If you do provideEquals(MyStruct other)than compiler will pick that method instead more genericEquals(Object)thus avoiding boxing for the struct.For all types you can provide much simple implementation of
Equals(MyType)as you already know the type. Also compiler/JIT may be able to optimize call better if method is not virtual because more specific version does not need to be virtual.