I have tried to peek into the code implemented for comparison operator in string class in C#. What found was this:
//THIS IS NOT WHAT I MEANT
public static bool Equals(object objA, object objB)
{
return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB)));
}
//THIS IS WHAT I SEE REALLY and the above is what I would expect to see
public static bool Equals(string a, string b)
{
return ((a == b) || (((a != null) && (b != null)) && EqualsHelper(a, b)));
}
public static bool operator ==(string a, string b)
{
return Equals(a, b);
}
I don’t know whether it is the Reflector that is playing tricks on me, but when I tried to implement this strategy for my own class, I got an infinite loop between Equals and the overloaded == operator (as expected). Is there sth different in string class or is it my Reflector that is reporting
static Equals(object o1, object o2)
method on the Object class to be part of String class?
Equality operators in C# are not polymorphic. When you evaluate
objA == objB, you are actually executing the==(object a, object b)operator implementation (which checks for reference equality), not the==(string a, string b), because the declared type of theobjAandobjBvariables isobject, notstring.The error you’re probably making in your code is that you’re not casting your class instances to
objectbefore evaluating the==operator on them.Assuming you have:
…you would need to replace it with:
…which is equivalent to:
Update: The
Stringclass contains both astatic bool Equals(string a, string b)method and astatic bool Equals(object a, object b)method. The difference is that the former is defined within theStringclass itself, whilst the latter is inherited from theObjectclass (which is the base class ofString). Your reflector might or might not display inherited methods based on its settings.In your posted code, since the declared type of
objAandobjBisobject, then the operator with theobjectparameters would get called, irrespective of the instances’ actual type.Update2: Your updated code does appear to contain an infinite recursion. I assume it might be a bug within the reflector tool.
Update3: This does appear to be a bug in the disassember. The first condition in the implementation of the
Equals(string a, string b)operator is shown, in disassembled C# code, as beinga == b. However, the first few lines of the IL code are actually:bne.un.sis defined as “Branch to the target instruction at the specified offset if two unsigned integer values are not equal (unsigned values), short form.”Thus, it appears that reference equality is being performed after all.