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?

Community
  • 1
  • 1
smohamed
  • 3,234
  • 4
  • 32
  • 57

2 Answers2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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. – Mike Christensen Nov 11 '11 at 17:53
  • @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. – Jon Skeet Nov 11 '11 at 17:54
  • `object.==` could be called in scenarios other than the one you mention, e.g. if one of the variables is typed as `IEnumerable`. – Ani Nov 11 '11 at 17:54
  • @Ani: Okay, will edit - basically "if the operands aren't both of type string". – Jon Skeet Nov 11 '11 at 17:57
-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.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • 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. – Jon Skeet Nov 11 '11 at 17:56
  • 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. – Mike Christensen Nov 11 '11 at 17:56
  • 1
    @MikeChristensen: That's not a good example, because in that case literal interning means that you *are* comparing two copies of the same reference. – Jon Skeet Nov 11 '11 at 17:56
  • @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? – James Johnson Nov 11 '11 at 18:25
  • @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`. – Jon Skeet Nov 11 '11 at 18:43
  • Sorry for bugging you about this, but I want to get it right despite the answer having been marked already. Have you discussed this anywhere before, or do you know of any good articles that discuss this topic? I'm also interested in an example where two distinct instances of string could evaluate to false. I appreciate your time. Thanks. – James Johnson Nov 11 '11 at 18:57
  • @JamesJohnson: Two distinct instances of string will be compared by reference equality if you compare variables which aren't of type `string`. For example: `object x = new StringBuilder("foo").ToString(); object y = new StringBuilder("foo").ToString(); bool z = x == y;` Now `z` will be false, because it will use the `==(object, object)` overload and just compare references. – Jon Skeet Nov 11 '11 at 19:15
  • As for other bits of writing about it - check out the duplicate questions linked from this one. – Jon Skeet Nov 11 '11 at 19:16
  • Oh, I see what you were saying now. I thought you meant something else. That makes sense. Thanks :) – James Johnson Nov 11 '11 at 19:18