String comparison doesn't work as expected

  • Thread starter Thread starter Zeng
  • Start date Start date
Z

Zeng

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 )
 
Zeng said:
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 )

I don't generally trust the debugger to display strings correctly.
See http://www.pobox.com/~skeet/csharp/strings.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).
 
Back
Top