473,378 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 2448
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ayende Rahien | last post by:
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...
1
by: Steve Norman | last post by:
Hi All, I am trying to display an int as it' sHex representation, and looking on MSDN it appears this can be done with .ToString("X"). This does work, but the example on MSDN...
5
by: Dan C Douglas | last post by:
I have just installed VS.NET 2003 on my computer. I have a project that I have been developing on VS.NET 2002. I haven't upgraded this project to VS.NET 2003 yet and I am still developing it in...
0
by: Norman Yuan | last post by:
I posted this issue a while ago. After moveing the ASP.NET 1.1 app to a brand new server, the same problem still there. System: Winows2003 server std. .NET 1.1 App: Invoicing system Problem:...
4
by: Larry Smith | last post by:
Once you call "ToString()" for an object, how can you then convert the string back to the original object (assuming you know its type of course)? Also, why does "ToString()" produce one string and...
4
by: Greg Collins [Microsoft MVP] | last post by:
Will DateTime.Now.ToString("s") ALWAYS return the same formatting (i.e. the sortable XML Schema style formatting) regardless of what culture the operating system is set to? I've done limited...
3
by: Senna | last post by:
Hi When you have something like this: decimal d = 17.4m; Response.Write(d.ToString("c")); The output becomes: 17,50 kr. If the culture is set to swedish. So my question is. Where in the...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.