This question already has an answer here:
- C# difference between == and Equals() 16 answers
I recently was introduced to a large codebase and noticed all string comparisons are done using String.Equals()
instead of ==
What's the reason for this, do you think?
There is practical difference between
string.Equals
and==
Adding Watch
Now look at
{1#}
and{5#}
obj
,str2
,str4
andobj2
references are same.obj
andobj2
areobject type
and others arestring type
Conclusion:
object
andstring
so performs a reference equality checkobject
andstring
so performs a reference equality checkobject
andstring
so performs a reference equality checkstring
andstring
so performs a string value checkstring
andstring
so performs a string value checkstring
andstring
so performs a string value checkobject
andobject
so performs a reference equality check - obj and obj2 point to the different references so the result is falseThere's a writeup on this article which you might find to be interesting, with some quotes from Jon Skeet. It seems like the use is pretty much the same.