How does String.Equals(a,b) not produce a StackOverflowException?

Your decompiler has a bug. The real code doesn't check a == b, it checks (Object)a == (Object)b, bypassing the overloaded operator.


Here is the real code from Microsoft. Operator == is implemented as

public static bool operator == (String a, String b) {
   return String.Equals(a, b);
}

operator == calls String.Equals which is implemented as:

public static bool Equals(String a, String b) {
    if ((Object)a==(Object)b) {
        return true;
    }

    if ((Object)a==null || (Object)b==null) {
        return false;
    }

    if (a.Length != b.Length)
        return false;

    return EqualsHelper(a, b);
}

As you see, the comparison for string equality is done using if ((Object)a==(Object)b) casting the string to object and then doing the comparison. So this will not call the overloaded operator == in string class.