string是引用类型或值类型 [英] string is a reference type or value type

查看:97
本文介绍了string是引用类型或值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以解释一下字符串是如何使用refence类型的吗?

Can explain me how the string is refence type?

推荐答案

虽然并非所有的值类型都有存储在堆栈,确实必须将一个字符串存储在堆上(这通常是作为原因给出的 - 字符串可能很大,因此它们无法存储在堆栈中)。



理论上,字符串很可能是值类型,但该值只是对字符串的引用。当.NET最初开发时,结构体效率非常低,语言设计者决定遵循Java惯例,即字符串不是本机类型,而是引用类型。



另外要考虑的一点 - 如果字符串是值类型然后将其转换为对象则需要非常低效的装箱操作。看一下这个 [ ^ ]文章,了解为什么这会不好。
While it's true that not all value types have to be stored on the stack, it is true that a string has to be stored on the heap (this is often given as the reason - string can be huge so they couldn't be stored on the stack).

In theory, a string could well have been a value type, but the value would simply have been the reference to the string. When .NET was first developed, structs were seriously inefficient and the language designers decided to follow the Java convention whereby a string was not a native type, but rather a reference type.

One other point to consider - if a string were a value type then converting it to an object would require a very inefficient boxing operation. Have a look at this[^] article to understand why this would be bad.


字符串是引用类型而不是值类型,它存储在堆上,而值类型存储在堆栈上。

String是一个引用类型,因为它知道引用的大小但不知道数据的大小,因此数据大小不是预定义的。

为了内存管理,字符串是不可变的,它们类似于值类型。

所以String在所有其他方面都是引用类型,除了是不可变的,StringBuilder是可变引用类型。
String is reference type not value type and it is stored on the heap whereas value type is stored on stack.
String is a reference type as it knows the size of the reference but not of the data and hence data size is not predefined.
Strings are immutable for the sake of memory management where they resemble the value types.
So String are reference type in all other aspects except being immutable and StringBuilder is the mutable reference type.


字符串是引用类型,但非常特殊。当您对两个字符串值进行操作时,例如在您的示例中

C = A,此操作中没有任何内容与正常的类行为相似。所以C没有A的引用。在该操作中,创建一个新的字符串类,并将字符串A中的值复制到字符串C.字符串C和字符串A指向完全不同的内存地址。这就是说,当你有很多字符串操作时,例如循环,并且在每次迭代中都有字符串操作,因为新的字符串类生成,该操作是性能泄漏。在那种情况下,它优先使用StringBuilder。为什么,因为StringBuilder没有为任何字符串操作创建新的字符串。



现在提问,为什么字符串不是值类型而且是引用类型。

当你让我们说int值时,Int类型定义了长度,即4字节。对于每种值类型,您都知道将采用的确切内存空间。但是对于字符串你不知道。拥有一个字符串类型是愚蠢的,你应该总是定义它的长度。如果你想在字符串变量中放入一个值更大的字符串,那么这个字符串是定义的,那将是不允许的。

为什么字符串是一个特殊的类。它很特殊,因为你有一个引用类,但使用它的方式看起来像一个值类型。所以,这就是你操作的原因:

string C = A,而不是字符串C = new string(A);

你没有实例字符串类因为.NET框架会在您第一次设置它时为您执行此操作。如果你没有设置它的值,则字符串的值为null,就像任何其他引用类型一样。每个语言平台都对字符串类型进行特殊处理。 .NET也不排除在外。
string is reference type but really special one. When you do operation of both string values like in your example
C = A, there is nothing in this operation simular to normal class behavior. So C doesn't have a reference of A. In that operation, a new string class is created and the value from string A is copied to string C. String C and String A are pointing in completly different memory address. That is way, when you have many string operation, for example loop and in every iteration you have a string manipulation, because of new string class generation, that operation is performance leak. In that situation it is preffered to use StringBuilder. Why, because StringBuilder doesn't create new string for any string operation.

Now for question, why string is not value type and it's reference type.
When you have let's say int value, Int type has defined lenght and that is 4 byte. And for every value type you know the exact memory space that will take. But for string you don't know that. It will be stupid to have a string type in wich you should always define how long it is. And if you want to put in that string variable a value with bigger lenght that string is defined, that will be disallowed.
Why is string a special class. It is special because you have a reference class, but the way of using it, looks like a value type. So, that's why you do operation like:
string C = A, and not string C = new string(A);
You don't have to instance string class because .NET framework do that for you when you first time set it's value. If you not set it's value string has value of null like any other reference type. Every language platform has a special treatment for string type. .NET is not exclusion from that too.


这篇关于string是引用类型或值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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