0

Possible Duplicate:
C#: String.Equals vs. ==
Are string.Equals() and == operator really same?

sometimes in a condition between two strings, I write:

if(string1==string2) //Do something

and sometimes I write:

if(string1.Equals(string2)) //Do something

The problem is sometimes the first one doesn't work, or miswork, is there any difference between the two expressions?

3
  • 1
    If string1 is null, a NullReferenceException will occur in the second one. Commented Nov 11, 2011 at 17:50
  • When does the first version of the code not work? Can you provide example operands? Commented Nov 11, 2011 at 17:50
  • sorry guys, the suggestion didn't bring that up when I was posting the question. Commented Nov 11, 2011 at 18:27

2 Answers 2

10

The first one will always work so long as the compile-time type of both operands is string.

If the compile-time type of either operand is anything other than string, it will use the normal reference identity comparison, rather than comparing strings for equality. Basically you want to call the ==(string, string) overload instead of the normal ==(object, object) overload.

Note that the first will succeed even if string1 is null, whereas the second will throw NullReferenceException in that case. An alternative in order to preserve the Equals call but avoiding this problem is to call the static object.Equals(object, object) method:

if (object.Equals(string1, string2))

Personally I'd just use == in cases where the compile-time types are appropriate though.

Sign up to request clarification or add additional context in comments.

4 Comments

String.Equals() also has an overload for StringComparison, which gives you a bit more control over how you want the strings compared. Something to note anyway I guess.
@MikeChristensen: Oh sure, if you're looking for other overloads - I was only talking about the code given, and the equivalent comparison used by the static method.
object.== could be called in scenarios other than the one you mention, e.g. if one of the variables is typed as IEnumerable<char>.
@Ani: Okay, will edit - basically "if the operands aren't both of type string".
-1

The use of == on two string types will perform a reference identity check, meaning it will only return true if both references point to the same object. Equals on the other hand is expected to perform a value comparison, and will return true if the references point to objects that are equivalent.

9 Comments

It can make a huge difference - which is why the compile-time types are important. If you use the overload of ==(string, string) then that will perform a value equality comparison, not a reference identity check. It's not clear what you mean by a "simple type" but I suspect you misunderstand something about the string type. It's entirely possible to have two equal but distinct instances of string, and if you use the ==(object, object) overload, that will return false.
I think this isn't quite true. "cat" == "cat" will return true even though they are not the same object. The real difference is == will call the static String method and myStr.Equals() will call the instance method (which is why it fails if the instance is null.
@MikeChristensen: That's not a good example, because in that case literal interning means that you are comparing two copies of the same reference.
@JonSkeet: Sometimes it's tough to account for all of the possible caveats. I think the wording is a little better now. Can you let me know if anything above is incorrect or if I've missed anything?
@JamesJohnson: It's not at all clear what you mean by "The standard use of == on a string type". If x and y are string variables, then x == y will perform the same test as Equals.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.