当“ToString”时不等于“ToString” [英] When "ToString" does not equal "ToString"
问题描述
我有一个非常奇怪的问题,在我的代码的某些部分我比较两个
字符串(通过对象),而我*知道*它们相等每个
其他,并且在观察窗口他们彼此相等,然后
比较返回false。
详情请见此处:
http ://www.ayende.com/Blog/PermaLink...c876a6acb.aspx
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
推荐答案
Ayende Rahien写道:
Ayende Rahien wrote:
我有一个非常奇怪的问题,在我的代码的某些部分我比较两个
字符串(通过对象),而我*知道*它们相等每个
其他,并且在观察窗口他们相互平等,然后
比较返回false。
详情请见这里:
http://www.ayende.com/Blog/PermaLink...34f-bfe0-39ac8
76a6acb.aspx
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
问题在于字符串既不是引用类型也不是值类型!
它们以特殊方式处理...
在您的情况下,您正在比较可能不同的对象引用!
使用string.Compare(a,b)或a.CompareTo(b)或a.Equals(b)
-
问候
Jochen
关于Win32和.NET的博客
http://blog.kalmbachnet.de/
失败的代码是
if(o == test)?
well o和test都输入为对象,因此您正在使用System.Object ==运算符,交给System.Object.ReferenceEquals。换句话说,如果o和test是相同的物理对象,则onyl会成功比较
问候
Richard Blewett - DevelopMentor > http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/< 29 ***************** ********@posting.google.com>
我有一个非常奇怪的问题,在我的代码的某些部分我比较两个
字符串(通过对象),虽然我知道*他们等于每个
其他,并且在观察窗口他们彼此相等,然后
比较返回false。
详见此处:
http://www.ayende.com/Blog/PermaLink...c876a6acb.aspx
---
Incomin g mail已通过免疫认证。
由AVG反病毒系统检查( http://www.grisoft.com)。
版本:6.0.771 /病毒库:518 - 发布日期:28/09/2004
[microsoft.public.dotnet.languages.csharp]
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]
嗯,字符串是引用类型。它们是不可变的,导致某些值像行为但它们肯定是引用类型(即在托管堆上分配,称为32位引用,并且是垃圾收集。
问候
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写道:
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:
我有一个非常奇怪的问题,在我的代码的某些部分我比较两个
字符串(通过对象),而我*知道*他们相等于其他人,并且在观察窗口他们彼此相等,然后
比较返回假。
详情请见此处:
http://www.ayende.com/Blog/PermaLink...34f-bfe0-39ac8
76a6acb.aspx
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
问题在于字符串既不是引用类型也不是值类型!
它们以特殊方式处理...
在您的情况下,您正在比较可能不同的对象引用!
使用string.Compare(a,b)或a.CompareTo(b)或a.Equals(b)
-
问候
Jochen
关于Win32和.NET的博客
http://blog.kalmbachnet.de/
---
接收邮件已通过无病毒认证。
由AVG反病毒系统检查( http://www.grisoft.com)。
版本:6.0.771 /病毒库:518 - 发布日期:28/09/2004
[microsoft.public.dotnet.languages.csharp]
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]
这篇关于当“ToString”时不等于“ToString”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!