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
There is no String.Equals(object, object)
method.
You're seeing Object.Equals
.
The reason it doesn't recurse is that objA == objB
calls the built-in object equality operator, not the custom string equality operator.
(Operator overloads are resolved based on the compile-time type of the operands)
A less confusing solution: don't use the ==
operator:
public static bool Equals(MyClass a, MyClass b)
{
return ReferenceEquals(a, b)
|| ((!ReferenceEquals(a, null) && !ReferenceEquals(b, null)) && a.Equals(b)));
}
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 the objA
and objB
variables is object
, not string
.
The error you’re probably making in your code is that you’re not casting your class instances to object
before evaluating the ==
operator on them.
Assuming you have:
public static bool Equals(MyClass objA, MyClass objB)
{
return objA == objB || objA != null && objB != null && objA.Equals(objB);
}
…you would need to replace it with:
public static bool Equals(MyClass objA, MyClass objB)
{
return (object)objA == (object)objB || objA != null && objB != null && objA.Equals(objB);
}
…which is equivalent to:
public static bool Equals(MyClass objA, MyClass objB)
{
return object.ReferenceEquals(objA, objB) || objA != null && objB != null && objA.Equals(objB);
}
Update: The String
class contains both a static bool Equals(string a, string b)
method and a static bool Equals(object a, object b)
method. The difference is that the former is defined within the String
class itself, whilst the latter is inherited from the Object
class (which is the base class of String
). Your reflector might or might not display inherited methods based on its settings.
In your posted code, since the declared type of objA
and objB
is object
, then the operator with the object
parameters 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 being a == b
. However, the first few lines of the IL code are actually:
ldarg.0
ldarg.1
bne.un.s IL_0006
ldc.i4.1
ret
bne.un.s is 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.
The equals method it refers to is this:
public static bool Equals(string a, string b)
{
/* == is the object equals- not the string equals */
return a == b || (a != null && b != null && string.EqualsHelper(a, b));
}
public static bool operator ==(string a, string b)
{
return string.Equals(a, b);
}
I.e. an equals method which takes two strings and not two objects.