Possible Duplicate:
C#: String.Equals vs. ==
Hi to all.
Some time someone told me that you should never compare strings with == and that you should use string.equals(), but it refers to java.
¿What is the diference beteen == and string.equals in .NET c#?
In C# there is no difference as the operator
==
and!=
have been overloaded in string type to callequals()
. See this MSDN page.The
==
operator calls theString.Equals
method. So at best you're saving a method call. Decompiled code:If you dont care about the string's case and dont worry about cultural awarenes then it's the same...
Look here for a better description. As one answer stated
string == string
is entirely the same asString.Equals
. This is the exact code (from Reflector):== actually ends up executing String.Equals on Strings.
You can specify a StringComparision when you use String.Equals....
Example:
Mostly, I consider it a coding preference. Use whichever you prefer.