I've seen in the souce code of the GUID type in the .NET Framework that the implementation of Equals and the ==
operator execute very similar code.
Why doesn't the ==
operator call the Equals method on the firts argument? Something like this:
public static bool operator ==(Guid a, Guid b)
{
return a.Equals(b);
}
The comment is very telling:
This doesn't make sense by itself. Looking for that comment elsewhere in the file:
but also
The comment only makes sense in that last method. This is a very strong indication that the
Guid.Equals(Object)
implementation came first.It would be bad, or at least sub-optimal, if
Guid.operator ==
andGuid.Equals(Guid)
were implemented on top ofGuid.Equals(Object)
, as that last method requires pointless allocations, and while it will hardly be noticeable in the common cases, there are certainlyGuid
comparisons that happen in some code in tight loops where the allocations would be measurable.Now, it would certainly have been possible to make
operator ==
useEquals(Guid)
, or vice versa, but it really isn't any additional work to copy and paste it twice instead of once.