Connecting Tech Pros Worldwide Forums | Help | Site Map

String comparison doesn't work as expected

Zeng
Guest
 
Posts: n/a
#1: Nov 17 '05
I looked at the watch window in the debugger CurrentThread.Name shows "" as
its value. Documentation shows that == operator of a string would test for
value. So does anyone know why this test evaluated to false? Thanks!

( "" == System.Threading.Thread.CurrentThread.Name )




Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Nov 17 '05

re: String comparison doesn't work as expected


Zeng <Zeng5000@hotmail.com> wrote:[color=blue]
> I looked at the watch window in the debugger CurrentThread.Name shows "" as
> its value. Documentation shows that == operator of a string would test for
> value. So does anyone know why this test evaluated to false? Thanks!
>
> ( "" == System.Threading.Thread.CurrentThread.Name )[/color]

I don't generally trust the debugger to display strings correctly.
See http://www.pobox.com/~skeet/csharp/s....html#debugger

Also, note that your way of doing comparisons "constant first" isn't
useful in C# - the more generally readable way (variable/property
first, constant second) still won't cause you problems if you have a
typo of "=" for "==" - because "if" expressions have to be boolean, you
could only end up doing an accidental assignment if the type of each
side was already boolean (in which case, comparisons with a constant
should usually just be expressed as if (foo) or if (!foo) anyway).

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread


Similar C# / C Sharp bytes