为什么字符串不像其他引用类型那样需要新的? [英] Why does string not require a new like other reference types?

查看:63
本文介绍了为什么字符串不像其他引用类型那样需要新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不敢相信为什么我之前没有注意到这一点以及为什么我之前从未问过它

...


定义字符串时为什么不需要使用new关键字?喜欢:


Dim a As String =新字符串


你可以在

构造函数但看起来字符串

类上没有空构造函数。


我觉得所有其他引用类型都很奇怪似乎需要一个新的

创建该类型的实例部分而String不会。


感谢您的任何见解......

I can''t believe why I had not noticed this before and why I never asked it
before...

When defining a string why is it not required to use the new keyword? Like:

Dim a As String = New String

You can do it when using the string type with some arguments in the
constructor but it look like there is no empty constructor on the String
class.

Seem odd to me that all the other reference types seem to require a new to
create the instance portion of the type and String does not.

Thanks for any insight...

推荐答案

Ray Cassick写道:
Ray Cassick wrote:

我不能相信为什么我之前没有注意到这一点为什么我之前从未问过它?
...


定义字符串时为什么不需要使用new关键字?喜欢:


Dim a As String =新字符串


你可以在

构造函数但看起来字符串

类上没有空构造函数。


我觉得所有其他引用类型都很奇怪似乎需要一个新的

创建该类型的实例部分而String不会。


感谢您的任何见解......
I can''t believe why I had not noticed this before and why I never asked it
before...

When defining a string why is it not required to use the new keyword? Like:

Dim a As String = New String

You can do it when using the string type with some arguments in the
constructor but it look like there is no empty constructor on the String
class.

Seem odd to me that all the other reference types seem to require a new to
create the instance portion of the type and String does not.

Thanks for any insight...



如果你声明一个字符串而没有为它赋值,你就没有创建任何字符串类的实例
,你只声明了一个

引用。


所有文字字符串值都在程序集中创建为常量。当代码启动时它们已经存在,你在分配代码时不需要使用

new关键字。


-

G?跑Andersson

_____
http://www.guffa.com


" Ray Cassick" < rc ****** @ enterprocity.comschrieb:
"Ray Cassick" <rc******@enterprocity.comschrieb:

定义字符串时为什么不需要使用新关键字?

喜欢:


Dim a As String =新字符串
When defining a string why is it not required to use the new keyword?
Like:

Dim a As String = New String



因为它没有意义。字符串在.NET中是不可变的。如果你用
写''Dim a As String =" Hello World'''你正在创建一个

''String''实例,其中包含Hello World和Hello World。并将其引用分配给

变量''a''。每个赋值给''String''变量都会改变引用。

Because it would not make sense. Strings are immutable in .NET. If you
write ''Dim a As String = "Hello World"'' you are creating an instance of
''String'' containing "Hello World" and assign a reference to it to the
variable ''a''. Each assignment to a ''String'' variable changes the reference.


你可以在
$ b中使用带有一些参数的字符串类型时这样做$ b构造函数但看起来字符串

类上没有空构造函数。
You can do it when using the string type with some arguments in the
constructor but it look like there is no empty constructor on the String
class.



使用无参数构造函数

构造的字符串应该有哪些值?


-

MS Herfried K. Wagner

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

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

Which value should a string constructed with the parameterless constructor
have?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>




" Herfried K. Wagner [MVP] QUOT; < hi *************** @ gmx.atwrote in message

news:ea ************** @ TK2MSFTNGP06.phx.gbl ...

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:ea**************@TK2MSFTNGP06.phx.gbl...

" Ray Cassick" < rc ****** @ enterprocity.comschrieb:
"Ray Cassick" <rc******@enterprocity.comschrieb:

>定义字符串时为什么不需要使用new关键字?
喜欢:

Dim a As String = New String
>When defining a string why is it not required to use the new keyword?
Like:

Dim a As String = New String



因为它没有意义。字符串在.NET中是不可变的。如果你用
写''Dim a As String =" Hello World'''你正在创建一个

''String''实例,其中包含Hello World和Hello World。并将其引用分配给

变量''a''。对''String''变量的每个赋值都会更改

引用。


Because it would not make sense. Strings are immutable in .NET. If you
write ''Dim a As String = "Hello World"'' you are creating an instance of
''String'' containing "Hello World" and assign a reference to it to the
variable ''a''. Each assignment to a ''String'' variable changes the
reference.


>您可以在使用字符串类型时执行此操作
构造函数中的一些参数,但它看起来像String
类上没有空构造函数。
>You can do it when using the string type with some arguments in the
constructor but it look like there is no empty constructor on the String
class.



使用无参数构造函数

构造的字符串应该使用哪个值?


Which value should a string constructed with the parameterless constructor
have?



感谢您的回复...


因此我理解它(以及实际教给我的方式)是这样的。


当你这样做:


Dim a As TypeName


存储在托管堆栈上声明。如果类型是ValueType

,则不需要其他任何内容,并且存储全部在堆栈上完成。如果

类型是一个引用类型,那么堆栈存储最终只是一个指针

,它最终将成为堆上某个地址,但指向

还没有。


当你这样做:


Dim a As ClassName = New ClassName ...


那么存储在堆栈上分配并且存储在

上分配,堆和堆栈指向堆上的正确位置。


所以,我想我的问题是..如果String是引用类型,你为什么不用每次使用New关键字来分配它?
需要分配它?几乎看起来好像

字符串是一个奇怪的情况。


我希望这样做:


Dim a As String


只会指向任何东西(就像它一样)就像这样做:


Dim a As Integer


结果为0.


我只是好奇为什么String类型似乎不一致

与其他参考类型相关的行为。

Thanks for the response...

So as I understood it (and the way it was actually taught to me) is this.

When you do this:

Dim a As TypeName

The storage is declared on the managed stack. IF the type is a ValueType
then nothing else is needed and the storage is all done on the stack. IF the
type is a reference type then the stack storage ends up just being a pointer
that will end up being an address on the heap somewhere but points to
nowhere just yet.

When you do this:

Dim a As ClassName = New ClassName...

THEN the storage is allocated on the stack AND the storage is allocated on
the heap and the stack points to the proper location on the heap.

So, I guess my question is.. if String is a reference type why do you NOT
NEED to allocate it using the New keyword every time? Almost seems like
strings are an odd case with respect to this.

I would expect that doing this:

Dim a As String

would simply point to nothing (as it does) just like doing this:

Dim a As Integer

results in a 0.

I am just curious as to why the String type seems to be inconsistent in
behavior with respect to other reference types.


这篇关于为什么字符串不像其他引用类型那样需要新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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