字符串..对象与否??? [英] Strings.. Objects or not???

查看:43
本文介绍了字符串..对象与否???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想知道为什么字符串'不是真正的对象?....让我解释一下......


如果我写代码


Dim x1 as String =" veg"

Dim x2 as String =" veg"


如果x1 = x2那么

''我希望这段代码能够被执行

结束如果


如果x1是x2那么

''我不希望这个代码被执行

结束如果


但是第二批代码被执行了!


这是正确的行为???如果是这样,那么字符串不正确

对象?


任何想法都将不胜感激!


Rigga。

Hi all,

I am wondering why string''s are not true objects?.... Let me explain...

If i write the code

Dim x1 as String = "veg"
Dim x2 as String = "veg"

If x1 = x2 then
'' i expect this code to be executed
End If

If x1 is x2 then
'' i do not expect this code to be executed
End If

However the second lot of code is executed!

Is this correct behavior??? if so is it true then that strings are not true
objects?

Any thoughts would be appreciated!

Rigga.

推荐答案

*" Rigga" < s@v.c> scripsit:
* "Rigga" <s@v.c> scripsit:
我想知道为什么字符串'不是真正的对象?....让我解释一下......

如果我写代码

Dim x1 as String =" veg"
Dim x2 as String =" veg"

如果x1 = x2那么
''我希望这段代码能够被执行
结束如果

如果x1是x2那么
''我不希望这段代码被执行
结束如果

然而第二批代码被执行了!

这是正确的行为???如果是这样,那么字符串不是真的
对象?
I am wondering why string''s are not true objects?.... Let me explain...

If i write the code

Dim x1 as String = "veg"
Dim x2 as String = "veg"

If x1 = x2 then
'' i expect this code to be executed
End If

If x1 is x2 then
'' i do not expect this code to be executed
End If

However the second lot of code is executed!

Is this correct behavior??? if so is it true then that strings are not true
objects?




为什么不呢?两个字符串对象都指向相同的常量。


-

Herfried K. Wagner [MVP]

< URL:http://dotnet.mvps.org/>



Why not? Both string objects are pointing to the same "constant".

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


我不买它......对我来说似乎很奇怪。使用IS运算符

表示生活在堆栈上的指针指向堆上的对象
。至少这就是我总是想象的......


这种行为让我觉得veg在堆上,并且x1 ans

x2都指向那个......但如果确实如此,则将X1

改为牛肉。也会改变X2,这当然不会发生。


它是否实际上创建了第三个对象,正如你所提到的那样,一个常量,在
堆上并指向那个?那么当我说x2 =牛肉时它在堆上创建了一个

的第二个常量对象,并将x2指向它,因此x1是

不再是x2 ....?


我很想听到关于这个问题的更多讨论...


--Zorpy


***通过Devdex发送 http://www.devdex.com ***

不要只是参加USENET ......获得奖励!
I don''t buy it... it seems odd to me as well. Using the IS operator
indicates that the pointer, living on the stack, points to the object on
the heap. At least that is how I always picture it...

This behavior makes me think that "veg" is on the heap, and both x1 ans
x2 are pointing to that... BUT if this were truely the case, changing X1
to "beef" would also change X2, which of course does not happen.

Does it actually create a third object, as you mention, a constant, on
the heap and point to that? then when I say x2="beef" it creates a
second constant object on the heap and points x2 to it, therefore x1 IS
no longer x2....?

I''d love to hear some more discussion on this question...

--Zorpy

*** Sent via Devdex http://www.devdex.com ***
Don''t just participate in USENET...get rewarded for it!


我相信String是一个真正的对象,但是它超载''= ''运算符。

所以当你编写代码x1 = x2时,x1和x2都是字符串,它将实际编译为x1是x2。如果您使用C#,您可以为自己的课程重载

运算符,并使它们的行为方式相同。


Lance

Rigga < s@v.c>在留言中写道

news:40 ********************* @ ptn-nntp-reader04.plus.net ...

大家好,


我想知道为什么字符串'不是真正的对象?....让我解释一下......


如果我写代码


Dim x1 as String =" veg"

Dim x2 as String =" veg"


如果x1 = x2那么

''我希望执行此代码

结束如果


如果x1是x2那么

''我不希望这个代码被执行

结束如果


然而第二批代码被执行了!


这是正确的行为???如果是这样,那么字符串不正确

对象?


任何想法都将不胜感激!


Rigga。

I believe the String is a true object, but it Overloads the ''='' Operator.
so when you write the code x1=x2 the and x1 and x2 are both strings, it will
actually compile the same as x1 is x2. If your using C#, you can overload
operators for your own classes, and make them behave the same way.

Lance

"Rigga" <s@v.c> wrote in message
news:40*********************@ptn-nntp-reader04.plus.net...
Hi all,

I am wondering why string''s are not true objects?.... Let me explain...

If i write the code

Dim x1 as String = "veg"
Dim x2 as String = "veg"

If x1 = x2 then
'' i expect this code to be executed
End If

If x1 is x2 then
'' i do not expect this code to be executed
End If

However the second lot of code is executed!

Is this correct behavior??? if so is it true then that strings are not true
objects?

Any thoughts would be appreciated!

Rigga.


这篇关于字符串..对象与否???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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