Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'==' vs string.equals c# .net [duplicate]

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#?

like image 684
Daniel Gomez Rico Avatar asked Apr 26 '11 20:04

Daniel Gomez Rico


People also ask

Can I use == to compare strings in 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.

Can you do == with a string?

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 .

Can C compare two characters?

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.

How do I compare two characters in a string?

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.


1 Answers

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
}
like image 73
Lasse Espeholt Avatar answered Oct 13 '22 00:10

Lasse Espeholt