'==' vs string.equals C # .net

Possible duplicate:
C #: String.Equals vs. ==

Hello everybody.

Sometime someone told me that you should never compare strings with == and that you should use string.equals (), but this applies to java.

¿What is the difference between == and string.equals in .NET C #?

+5
source share
8 answers

string == stringcoincides with String.Equals. This is the exact code (from Reflector):

public static bool operator ==(string a, string b)
{
    return Equals(a, b); // Is String.Equals as this method is inside String
}
+15
source

In C #, there is no difference, as the operator ==, and !=was overwhelmed in a string type to trigger equals(). See This MSDN Page .

+3
source

== String.Equals on Strings.

StringComparision String.Equals....

:

MyString.Equals("TestString", StringComparison.InvariantCultureIgnoreCase)

, . , .

+3

== String.Equals. . :

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

.

== ,   System.Object.ReferenceEquals.

Equals - , ( ).

+1

it makes no difference, it's just an operator overload. for strings, this is internally the same. however, you do not want to get used to using == to compare objects and why it is not recommended to use it for strings.

0
source

In C # there is no difference for strings.

0
source

If you don’t care about the lowercase case and don’t worry about cultural awarenes, then this is the same ...

0
source

All Articles