为什么要转换为Object来比较字符串? [英] Why cast to Object to compare strings?

查看:75
本文介绍了为什么要转换为Object来比较字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为什么要将两个字符串转换为对象来比较它们?我在MSDN上的MS样本中看到了代码

但是没有得到它。


if((object)name ==(object)attr。姓名){


bothname和attr.name被声明为字符串。

http://msdn.microsoft.com/XML/Buildi...mlBkMkRead.asp


谢谢,


Sam

B-Line现在正在招聘一个华盛顿特区的VB.NET

开发者用于WinForms + WebServices位置。

中级到高级开发人员。对于

信息或将电子邮件简历应用于

sam_blinex_com。

解决方案



" Samuel R. Neff" < BL **** @ newsgroup.nospam>在消息中写道

新闻:ia ******************************** @ 4ax.com ...


为什么要将两个字符串转换为对象来比较它们?我在MSDN上的MS示例中看到了代码
并且没有得到它。

if((object)name ==(object)attr.name){
<两者都是名称和attr.name声明为字符串。


绕过运算符重载。通过在这里转换为object,你

强制编译器使用objects == operator,这是一个参考

比较,而不是cast会使用strings == operator,

是字符串比较(字符比较的完整字符)。我不知道

特别是为什么作者觉得参考比较在这里很重要(我在你的例子中找不到
),但这就是效果。


我个人更喜欢使用object.ReferenceEquals()而不是将

两个实例转换为对象。 http://msdn.microsoft.com/XML/Buildi...mlBkMkRead.asp

谢谢,

Sam

B-Line现在正在招聘一名华盛顿特区的VB.NET开发人员,负责WinForms + WebServices的职位。
中级到高级开发人员。有关
信息或将电子邮件简历应用于
sam_blinex_com。





谢谢,我没有意识到施放受影响的操作员超载

分辨率。


希望我知道为什么作者想要使用参考比较
$ b $但是,它很奇怪。


祝你好运,


Sam

星期二,22 2005年3月16:07:14 -0600,Daniel O''Connell [C#MVP]"

< onyxkirx @ - NOSPAM--comcast.net>写道:


Samuel R. Neff < BL **** @ newsgroup.nospam>在消息中写道
新闻:ia ******************************** @ 4ax.com ..。< blockquote class =post_quotes>
为什么要将两个字符串转换为对象来比较它们?我在MSDN上的MS示例中看到了代码
并且没有得到它。

if((object)name ==(object)attr.name){
<两者都是名称和attr.name声明为字符串。



是绕过运算符重载。通过在这里转换为object,你强制编译器使用objects == operator,这是一个参考比较,而不是cast会使用strings == operator,这是一个字符串比较(完整字符与字符比较)。我不知道
具体为什么作者认为参考比较在这里很重要(我在你的例子中找不到它),但那就是效果。
我个人更喜欢使用object.ReferenceEquals()而不是将两个实例转换为对象。

http://msdn.microsoft.com/XML/Buildi .. .mlBkMkRead.asp

谢谢,

Sam



B-Line正在招聘一名华盛顿特区VB.NET

开发者为WinForms + WebServices职位。

中级到高级开发人员。对于

信息或将电子邮件简历应用于

sam_blinex_com。


是否使用character-by比较字符串 - 字符比较?由于

CLR执行字符串折叠,所以不是所有相同的字符串真的是

相同的副本吗?所以没有==对于字符串只是做一个参考

比较,因为它对对象呢?


我没有看到比较点

你知道所有对相同字符串的引用指向相同的

内存位置......



Why would you cast two strings to objects to compare them? I saw code
in an MS sample on MSDN and don''t get it.

if ( (object)name == (object)attr.name ) {

both "name" and "attr.name" are declared as string.

http://msdn.microsoft.com/XML/Buildi...mlBkMkRead.asp

Thanks,

Sam
B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.

解决方案


"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:ia********************************@4ax.com...


Why would you cast two strings to objects to compare them? I saw code
in an MS sample on MSDN and don''t get it.

if ( (object)name == (object)attr.name ) {

both "name" and "attr.name" are declared as string.

It is to circumvent operator overloading. By casting to object here, you
force the compiler to use objects == operator, which is a reference
comparison, whereas not casting would have used strings == operator, which
is a string comparison(full character to character compare). I don''t know
specifically why the author felt reference comparison was important here(I
can''t find it in your example), but that is the effect.

I personally prefer to use object.ReferenceEquals() instead of casting the
two instances to object. http://msdn.microsoft.com/XML/Buildi...mlBkMkRead.asp

Thanks,

Sam
B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.




Thanks, I didn''t realize casting affected operator overload
resolution.

Wish I knew why the writer wanted to use reference comparison for
strings though, it''s weird.

Best regards,

Sam
On Tue, 22 Mar 2005 16:07:14 -0600, "Daniel O''Connell [C# MVP]"
<onyxkirx@--NOSPAM--comcast.net> wrote:


"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:ia********************************@4ax.com.. .


Why would you cast two strings to objects to compare them? I saw code
in an MS sample on MSDN and don''t get it.

if ( (object)name == (object)attr.name ) {

both "name" and "attr.name" are declared as string.



It is to circumvent operator overloading. By casting to object here, you
force the compiler to use objects == operator, which is a reference
comparison, whereas not casting would have used strings == operator, which
is a string comparison(full character to character compare). I don''t know
specifically why the author felt reference comparison was important here(I
can''t find it in your example), but that is the effect.

I personally prefer to use object.ReferenceEquals() instead of casting the
two instances to object.

http://msdn.microsoft.com/XML/Buildi...mlBkMkRead.asp

Thanks,

Sam


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.


Are strings compared using character-by-character comparison? Since the
CLR performs string folding, aren''t all identical strings really the
same copy anyway? So doesn''t == for strings just do a reference
comparison as it does for object?

I don''t see the point of comparing strings character-by-character when
you know that all references to identical strings point to the same
memory location...?


这篇关于为什么要转换为Object来比较字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆