4

A friend of mine came across the interesting source code of the two methods in String.cs:

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool operator ==(string a, string b)
{
    return Equals(a, b); 
}

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool Equals(string a, string b)
{
    return ((a == b) || (((a != null) && (b != null)) && EqualsHelper(a, b)));
}

Why doesn't it result in an endless loop? (and all our programs will be terminated by a StackOverflowException!)

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174

2 Answers2

0

Apparently so, at least according to the accepted answer.

(I can't leave a comment until I have a certain amount of rep. Hooray SO.)

Shoaib
  • 561
  • 3
  • 11
  • Well now there's an infinite loop of accepted answer citation. EDIT: oh, you meant on the linked post :) – Paul Bellora Jun 07 '12 at 04:01
  • Haha right - this was meant to be a reply to Danny's comment on the question. Ironically, my original comment on the question was meant to be an answer, but SO decided it was trivial and converted it to a comment. – Shoaib Jun 07 '12 at 12:13
0

You beat me to it Shoaib. I was trying to slip in my first answered question as well when your answer was posted. :)

It sounds like what's missing is a cast to "object", which would force the compiler to use the Equals method from Object, and which would prevent an infinite loop.

Malaise
  • 705
  • 3
  • 8
  • 20