On May 9, 6:40*pm, not_a_commie <notacom...@gmail.com> wrote:
> > Better to use if (!string1.equals(string2)) than if(string1!=string2),
> > if you are checking contents to be equal
> > you need to take care of null values too.
>
> I'm pretty sure the string class overloads the '!=' and '==' to
> compare references and then contents if the reference comparison
No it doesnt, it works because of string "intern pool" the .Net
provides for optimization.
So if you have two string literals containing same string value, then
most probably both of them references to same string literal in intern
pool.
But it is never safe.
> fails. Is that not the case? You have to beware of nulls for what you
> are suggesting.
Yes that is the pain you need to take for good.
Cheers,
-Ratnesh