Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Equality operator == in c#

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?

like image 852
Bober02 Avatar asked Dec 07 '22 16:12

Bober02


1 Answers

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.

like image 160
Douglas Avatar answered Dec 20 '22 14:12

Douglas