String Equality operator == in c#

青春壹個敷衍的年華 提交于 2019-12-04 04:34:13

问题


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?


回答1:


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.




回答2:


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)




回答3:


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))); 
} 



回答4:


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.



来源:https://stackoverflow.com/questions/9317737/string-equality-operator-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!