== operator and Equals for String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sergei Gnezdov

    #1

    == operator and Equals for String

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

    Object str1 = ...
    Object str2 = ...
    Assertion.Asser t(str1.Equals(s tr2)); // passes, str1 equals str2
    Assertion.Asser t(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/de...tyoperator.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
  • Jon Skeet

    #2
    Re: == operator and Equals for String

    Sergei Gnezdov <sergei-gnezdov-pub@pobox.com> wrote:[color=blue]
    > I've got code in NUnit which compares two strings.
    >
    > Object str1 = ...
    > Object str2 = ...
    > Assertion.Asser t(str1.Equals(s tr2)); // passes, str1 equals str2
    > Assertion.Asser t(str1 == str2); // fails!
    >
    > The following link states that str1 == str2 should compare values and
    > thus == operator should not return false in my case:[/color]

    No it shouldn't, because your variables are declared as object
    references, not string references. Operators are not applied
    polymorphically .
    [color=blue]
    > 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.[/color]

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

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...