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, string values (including string literals) are represented as arrays of char followed by a 0 terminator, and you cannot use the == operator to compare array contents; the language simply doesn't define the operation.
In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .
We can compare two strings in C using a variety of approaches. The two strings to be checked must be compared character by character. We can compare two strings using strcmp() string library function which returns 0 if two strings are not equal.
Syntax: str1.equals(str2); Here str1 and str2 both are the strings which are to be compared. Using String.equalsIgnoreCase() : The String.equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string.
string == string
is entirely the same as 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With