Infinite call loop between String.Equals and operator ==

My friend came across an interesting source code of 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 does this not lead to an infinite loop? (and all of our programs will be aborted with a StackOverflowException!)

+4
source share
2 answers

Apparently, so, at least in accordance with the accepted answer.

(I cannot leave a comment until I have a certain reputation. Hooray SO.)

0
source

You beat me with this Shoaib. I tried to slip into my first answer to the question when your answer was posted. :)

It seems that there is no cast to the object, which caused the compiler to use the Equals method from Object and which would prevent an infinite loop.

0
source

Source: https://habr.com/ru/post/1416502/


All Articles