== operator and Equals for String

  • Thread starter Thread starter Sergei Gnezdov
  • Start date Start date
S

Sergei Gnezdov

I've got code in NUnit which compares two strings.

Object str1 = ...
Object str2 = ...
Assertion.Assert(str1.Equals(str2)); // passes, str1 equals str2
Assertion.Assert(str1 == str2); // fails!

The following link states that str1 == str2 should compare values and
thus == operator should not return false in my case:
http://msdn.microsoft.com/library/d...ry/en-us/csref/html/vclrfequalityoperator.asp

More magic happens if I work from VisualStudio .NET 2003. If I stop
code execution in debugger for the code like this:

Boolean equals = str1 == str2

I will see that equals is false. If I execute str1 == str2 from
command window in immediate mode I will see true!

Can anybody explain to me what is going on.


Thank you
 
Sergei Gnezdov said:
I've got code in NUnit which compares two strings.

Object str1 = ...
Object str2 = ...
Assertion.Assert(str1.Equals(str2)); // passes, str1 equals str2
Assertion.Assert(str1 == str2); // fails!

The following link states that str1 == str2 should compare values and
thus == operator should not return false in my case:

No it shouldn't, because your variables are declared as object
references, not string references. Operators are not applied
polymorphically.
More magic happens if I work from VisualStudio .NET 2003. If I stop
code execution in debugger for the code like this:

Boolean equals = str1 == str2

I will see that equals is false. If I execute str1 == str2 from
command window in immediate mode I will see true!

Can anybody explain to me what is going on.

I'd rarely trust the debugger doing this kind of thing, to be honest...
 
Back
Top