473,748 Members | 5,242 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2472
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.R eferenceEquals. 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.publi c.dotnet.langua ges.csharp/<29************ *************@p osting.google.c om>

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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.csharp/<Xn************ *************** *******@207.46. 248.16>

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.publi c.dotnet.langua ges.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.co m>
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.R eferenceEquals. 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.publi c.dotnet.langua ges.csharp/<29************ *************@p osting.google.c om>

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.publi c.dotnet.langua ges.csharp]

Nov 16 '05 #7
"Ayende Rahien" <Ayende Ra****@discussi ons.microsoft.c om> wrote in message
news:5B******** *************** ***********@mic rosoft.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.publi c.dotnet.langua ges.csharp/<5B************ *************** *******@microso ft.com>

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.R eferenceEquals. 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3163
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 the comparision return false. See here for details: http://www.ayende.com/Blog/PermaLink,guid,f5aa1a11-1710-434f-bfe0-39ac876a6acb.aspx
1
39912
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 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemenumclasstostringtopic.asp) the int is formatted with leading 0's to make an 8 char string, but when I use it there are no leading 0's.
5
2868
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 VS.NET 2002. When I am putting values from my SQLDataReader into labels and text boxes I am getting results such as 400.0000, or 25.90. For example... ? dr("CostsPerBin") returns
0
1639
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: Since the app is dealing with money amount a lot, in many place I used Double.ToString("C") to show a number as money amount on the web page. There
4
4877
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 "TypeConverter.ConvertToString()" produce yet another (e.g., for a "System.Drawing.Size" object, the former method yields, say, "Width=5, Height=40" and the latter yields "5, 40"). Thanks.
4
14267
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 testing to show that this is the case when switching to the Italian culture, but I need to verify that in EVERY culture setting it will ALWAYS return the same format. Thanks. -- Greg Collins Visit Brain Trove ( http://www.BrainTrove.com )
3
2744
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 framework is the class/function that does
6
9112
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' is odd However, if format.NumberDecimalDigits is 1 decimal d = 3.45M; d.ToString( "F", format ); //Return 3.5 - this is different from Math.Round;
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.