4

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?

Bober02
  • 15,034
  • 31
  • 92
  • 178
  • What does the Reflector say about the objA.Equals method? It can't be the same as the static Equals method because it's an instance method. Based on the code you supplied, I don't see an infinite loop. Can you show us the instance Equals method? – qxn Feb 16 '12 at 19:31
  • Sorry I made an update on this question, I posted a wrong definition... – Bober02 Feb 16 '12 at 19:35
  • 1
    The infinite in my opnion, happens when (a==b) is called in Equals(string a, string b). The STATIC type of a and b are strings so the Overloaded operator == is called.I checked it on my simple example and I am now not sure whether there is sth here I don't see or is it Reflector that lies. – Bober02 Feb 16 '12 at 19:40
  • You can avoid all this confusion by not using the `==` operator at all. Use `ReferenceEquals` to check for reference equality; it's far less ambiguous. – phoog Feb 16 '12 at 19:49

4 Answers4

8

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.

Douglas
  • 53,759
  • 13
  • 140
  • 188
7

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)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • In a way, there _is_ a `String.Equals(object, object)` method; it’s just not defined within the `String` class, but inherited from the `Object` class. – Douglas Feb 16 '12 at 19:37
  • Yes but mine has the parameter types of strings, which would make another call to overloaded == operator and this one would call Equals method again and so on... – Bober02 Feb 16 '12 at 19:41
  • @Douglas static methods are not inherited. You cannot call `String.Equals(object, object)`. The static `Object.Equals(object, object)` method is *in scope* in the string class, so it can be called without the `Object.` part, but it is not inherited by it. – phoog Feb 16 '12 at 19:46
  • @phoog: I’m afraid you might be mistaken. `bool b = String.Equals(new object(), new object());` compiles (and evaluates to `false`). – Douglas Feb 16 '12 at 20:00
  • @Douglas you're right. I must have misunderstood or misremembered a conversation I had about this earlier in the week. – phoog Feb 16 '12 at 20:05
  • @phoog: Up to a few minutes ago, I would have guessed the same thing. I just happened to see the inherited static method through IntelliSense whilst experimenting with Bober02’s issue. – Douglas Feb 16 '12 at 20:15
  • @Douglas the conversation, I now remember, was about generic type parameters. You cannot call static methods on generic type parameters, even if `T` has a type constraint of the type that defines the method. – phoog Feb 16 '12 at 20:21
  • @phoog: True. But given that static methods are not polymorphic, wouldn’t calling the static method on the generic type (had it been allowed) always be equivalent to calling the static method on the type constraint? – Douglas Feb 16 '12 at 20:28
  • @Douglas for the most part, though there's also the possibility of hiding. To solve that, you need to use reflection. – phoog Feb 16 '12 at 20:31
  • @phoog: I can see the benefit, but I don’t think it would have been possible to resolve the `new` member at compile-time, since the compiler knows nothing about the generic type beyond the type constraint. As you mentioned, that is a run-time consideration best handled through reflection. – Douglas Feb 16 '12 at 20:36
1

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))); 
} 
phoog
  • 42,068
  • 6
  • 79
  • 117
0

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.

Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99