Why would you use String.Equals over ==?

JamesBrownIsDead picture JamesBrownIsDead · Nov 2, 2009 · Viewed 298.9k times · Source

I recently was introduced to a large codebase and noticed all string comparisons are done using String.Equals() instead of ==

What's the reason for this, do you think?

Answer

Matthew Scharley picture Matthew Scharley · Nov 2, 2009

It's entirely likely that a large portion of the developer base comes from a Java background where using == to compare strings is wrong and doesn't work.

In C# there's no (practical) difference (for strings) as long as they are typed as string.

If they are typed as object or T then see other answers here that talk about generic methods or operator overloading as there you definitely want to use the Equals method.