469,608 Members | 1,515 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,608 developers. It's quick & easy.

When "ToString" does not equal "ToString"

I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...c876a6acb.aspx
Nov 16 '05 #1
11 2323
Ayende Rahien wrote:
I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...34f-bfe0-39ac8
76a6acb.aspx


The problem is that "strings" are neither reference types nor value types!
They are handled in a special way...

In your case you are comparing object references which might be different!
Either use string.Compare(a, b) or a.CompareTo(b) or a.Equals(b)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 16 '05 #2
The code that is failing is the

if( o == test) ?

well o and test are both typed as object so you are using the System.Object == operator which hands off to System.Object.ReferenceEquals. In other words, the comparison will onyl succeed if o and test are teh same physical object

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<29*************************@posting.google.com>

I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...c876a6acb.aspx

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #3
Umm, strings are reference types. They are immutable which results in some value like behavior but they are definitely reference types (i.e. allocated on the managed heap, referred to vai a 32 bit reference and are garbage collected.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<Xn**********************************@207.46.248.1 6>

Ayende Rahien wrote:
I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...34f-bfe0-39ac8
76a6acb.aspx


The problem is that "strings" are neither reference types nor value types!
They are handled in a special way...

In your case you are comparing object references which might be different!
Either use string.Compare(a, b) or a.CompareTo(b) or a.Equals(b)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #4
Jochen Kalmbach <no********************@holzma.de> wrote:
Ayende Rahien wrote:
I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...34f-bfe0-39ac8
76a6acb.aspx


The problem is that "strings" are neither reference types nor value types!
They are handled in a special way...


That's not true. String is a reference type which overrides operator==.

In what way do you believe they're not reference types?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon Skeet [C# MVP] wrote:
> http://www.ayende.com/Blog/PermaLink...0-434f-bfe0-39
> ac8 76a6acb.aspx


The problem is that "strings" are neither reference types nor value
types! They are handled in a special way...


That's not true. String is a reference type which overrides
operator==.

In what way do you believe they're not reference types?


As Richard explains:
They are immutable which results in some value like behavior but they are
definitely reference types.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 16 '05 #6
Try here for a full sample, this should *never* happen.

http://www.ayende.com/Blog/PermaLink...f4d261dc0.aspx

"Richard Blewett [DevelopMentor]" wrote:
The code that is failing is the

if( o == test) ?

well o and test are both typed as object so you are using the System.Object == operator which hands off to System.Object.ReferenceEquals. In other words, the comparison will onyl succeed if o and test are teh same physical object

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<29*************************@posting.google.com>

I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...c876a6acb.aspx

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #7
"Ayende Rahien" <Ayende Ra****@discussions.microsoft.com> wrote in message
news:5B**********************************@microsof t.com...
Try here for a full sample, this should *never* happen.


Your full sample seems to show exactly what Richard said. When they are
compared as objects, they are different, and when compared as strings, they
are the same. This is exactly the behavior expected.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 16 '05 #8
Ummm - thats where I went to find the if( o == test) piece of code. What do you mean by "this should never happen"? If you have two System.Object references compared by using the == operator then it will erform a reference check as I said becasue operator overloads are not virtual.

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<5B**********************************@microsoft.co m>

Try here for a full sample, this should *never* happen.
http://www.ayende.com/Blog/PermaLink...f4d261dc0.aspx

"Richard Blewett [DevelopMentor]" wrote:
The code that is failing is the

if( o == test) ?

well o and test are both typed as object so you are using the System.Object == operator which hands off to System.Object.ReferenceEquals. In other words, the comparison will onyl succeed if o and test are teh same physical object

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


Nov 16 '05 #9
Jochen Kalmbach <no********************@holzma.de> wrote:
The problem is that "strings" are neither reference types nor value
types! They are handled in a special way...


That's not true. String is a reference type which overrides
operator==.

In what way do you believe they're not reference types?


As Richard explains:
They are immutable which results in some value like behavior but they are
definitely reference types.


Right. There's nothing particularly special about string being
immutable though - it's perfectly easy to write your own immutable
reference types, and it's often a good idea.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
<"=?Utf-8?B?QXllbmRlIFJhaGllbg==?=" <Ayende
Ra****@discussions.microsoft.com>> wrote:
Try here for a full sample, this should *never* happen.

http://www.ayende.com/Blog/PermaLink...-4f48-9375-1c6
f4d261dc0.aspx


As Richard said, there's nothing mysterious here.

Just remember that operator overloading is considered at *compile*
time, not *run* time. (It's overloading, not overriding.) The compiler
doesn't know that o's value is actually going to be a reference to a
string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
Ayende Rahien wrote:
I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...c876a6acb.aspx


You are actually comparing two System.Object reference's, which IIRC
only checks if the two variables are referencing the same object.

Now, if one of your strings is not interned, this will fail. Try the
following:

foreach(Object o in enumerable)
{
string s1 = (string)o;
string s2 = (string)test;

if(String.IsInterned(s1) == null)
Console.WriteLine("s1 is not interned!");

if(String.IsInterned(s2) == null)
Console.WriteLine("s2 is not interned!");

if (o==test)
return;
}

If any of the two IsInterned checks fail, the comparison will fail.

HTH

Joakim
Nov 16 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ayende Rahien | last post: by
1 post views Thread by Steve Norman | last post: by
5 posts views Thread by Dan C Douglas | last post: by
4 posts views Thread by Larry Smith | last post: by
4 posts views Thread by Greg Collins [Microsoft MVP] | last post: by
3 posts views Thread by Senna | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.