.net 字符串是值类型还是引用类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1069155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is string a value type or a reference type?
提问by Schwertz
Is string a value type or a reference type?
字符串是值类型还是引用类型?
I just can't find a "good" explanation for this...
我只是找不到对此的“好”解释......
回答by Marc Gravell
Console.WriteLine(typeof(string).IsClass); // true
It's a reference type.
它是一个引用类型。
It can't be a value-type, as value-types need a known size for the stack etc. As a reference-type, the size of the referenceis known in advance, even if the size of the string isn't.
它不能是数值类型,作为值类型需要的堆栈等公知的大小作为参考型时,大小参考事先是已知的,即使所述字符串的大小是没有的。
It behaveslike you expect a value-type to behave because it is immutable; i.e. it doesn't* change once created. But there are lots of other immutable reference-types. Delegate instances, for example.
它的行为就像您期望值类型的行为一样,因为它是不可变的;即它一旦创建就不会* 改变。但是还有很多其他不可变的引用类型。例如,委托实例。
*=except for inside StringBuilder, but you never see it while it is doing this...
*=除了 inside StringBuilder,但是在执行此操作时您永远不会看到它...
回答by kemiller2002
String is an immutable reference type.
字符串是不可变的引用类型。
See the blog post Immutable types: understand their benefits and use themon immutability.
请参阅博客文章不可变类型:了解它们的好处并在不变性上使用它们。
回答by Darin Dimitrov
回答by Charles Bretana
The fundamental "explanation" is based on "what" is actually stored in the memory location allocated when you "declare" the variable for the thing. If the actual value of the thing is stored in the memory location referred to by the variable name, then it is a value type.
基本的“解释”基于“声明”事物的变量时实际存储在分配的内存位置中的“内容”。如果事物的实际值存储在变量名所指的内存位置,那么它就是值类型。
int x; // memory allocated to hold Value of x, default value assigned of zero
If, otoh, the memory slot allocated when you "declare" the variable will hold only some othermemory address where the actual value (or values) will be stored, then it is a reference type.
如果,哦,当您“声明”变量时分配的内存槽将只保存一些其他内存地址,其中实际值(或多个值)将被存储,那么它是一个引用类型。
MyClass x; // Memory allocated to hold an address,
// default address of null (0) assigned.
// NO MEMORY ALLOCATED for x itself
or, if declaration includes initialization,
或者,如果声明包括初始化,
MyClass x = new MyClass();
// Now, Memory slot (call it Addr1) is allocated to hold address of x,
// more memory (call it Addr2) is allocated to hold a new MyClass object.
// New MyClass object created, stored in memory Addr2 (on the Heap)
// Address of new object (Addr2) is stored in Addr1
for a string, the string is created on the Heap, and it's address goes in the memory slot allocated for the variable, so it is a reference type.
对于字符串,该字符串是在堆上创建的,它的地址位于为变量分配的内存槽中,因此它是一种引用类型。
回答by JaredPar
String is an immutable reference type which has certain qualities that give it the occasional appearance of being a value type
String 是一种不可变的引用类型,它具有某些特性,使其偶尔出现作为值类型
回答by Sonu Rajpoot
The string type represents a sequence of zero or more Unicode characters. string is an alias for String in the .NET Framework.
字符串类型表示零个或多个 Unicode 字符的序列。string 是 .NET Framework 中 String 的别名。
Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. This makes testing for string equality more intuitive. For example:
尽管字符串是引用类型,但相等运算符(== 和 !=)被定义为比较字符串对象的值,而不是引用。这使得对字符串相等性的测试更加直观。例如:
string a = "hello";
string b = "h";
// Append to contents of 'b'
b += "ello";
Console.WriteLine(a == b);
Console.WriteLine((object)a == (object)b);
This displays "True" and then "False" because the content of the strings are equivalent, but a and b do not refer to the same string instance.
这将显示“True”然后显示“False”,因为字符串的内容是等效的,但 a 和 b 不引用相同的字符串实例。
Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and that new object is assigned to b. The string "h" is then eligible for garbage collection.
字符串是不可变的——字符串对象的内容在对象创建后不能更改,尽管语法使它看起来好像可以这样做。例如,当您编写此代码时,编译器实际上会创建一个新的字符串对象来保存新的字符序列,并将该新对象分配给 b。字符串“h”然后有资格进行垃圾收集。
string b = "h";
b += "ello";
回答by supercat
In many languages, there are two general types of things: those where a storage location of a defined type will actually hold an object of that type, and those where a storage location of a defined type will hold a reference to an object of that type which is stored elsewhere. There are also a number of types of semantics things may offer:
在许多语言中,有两种一般类型的事物:一种是已定义类型的存储位置实际上将保存该类型的对象,另一种是已定义类型的存储位置将保存对该类型对象的引用存储在其他地方。事物还可以提供多种类型的语义:
- Immutable value semantics: instances of a particular type have some characteristic ("the value") which forms the basis of identity. Two items whose value is equal may be used interchangeably, regardless of where they are stored. The value will be constant as long as the instance exists. A variable of such a type may have its value changed, but only by storing a different instance into the variable.
- Immutable reference semantics: generally similar to immutable value semantics, except that two instances which were created at different times will report themselves as being different instances.
- Mutable value semantics: instances of a particular type have some characteristic or collection of characteristics ("the values") which forms the basis of identity, but those characteristics may be changed without replacing the entire instance. Every instance is stored in precisely one variable or field; copying one variable or field to another copies all the values from one the first instance to the second, but the instances remain separate. Future changes to one instance will not affect the other.
- Mutable reference semantics: every instance is identical to itself, but to no other entity, and instances have one or more values which may be changed within existing instances. Any number of variables or fields may hold a reference to any instance. Copying one or field variable to another simply makes the second refer to the same instance as the first. Consequently, any change to the instance referred to by one of the variables will affect the instance referred to by the other (i.e. the same instance).
- 不可变值语义:特定类型的实例具有构成身份基础的某些特征(“值”)。两个值相等的项目可以互换使用,无论它们存储在哪里。只要实例存在,该值就会保持不变。这种类型的变量可能会改变其值,但只能通过将不同的实例存储到变量中。
- 不可变引用语义:通常类似于不可变值语义,除了在不同时间创建的两个实例将自己报告为不同的实例。
- 可变值语义:特定类型的实例具有一些特征或特征集合(“值”),它们构成了身份的基础,但这些特征可以在不替换整个实例的情况下进行更改。每个实例都精确地存储在一个变量或字段中;将一个变量或字段复制到另一个变量或字段会将所有值从第一个实例复制到第二个实例,但这些实例保持独立。未来对一个实例的更改不会影响另一个。
- 可变引用语义:每个实例与自身相同,但不与其他实体相同,并且实例具有一个或多个可以在现有实例中更改的值。任何数量的变量或字段都可以保存对任何实例的引用。将一个或字段变量复制到另一个只会使第二个引用与第一个相同的实例。因此,对变量之一引用的实例的任何更改都将影响另一个引用的实例(即相同的实例)。
In some programming languages like C++, it is possible for both direct-stored and indirect-reference types to implement any of the above four types of semantics. In .net and C#, direct-stored types with exposed fields always implement #3, class types with exposed fields always implement #4, other value types may implement any of the above, and other reference types may implement #1, #2, or #4, but not #3, with an only-slightly-leaky abstraction. Strings implement #1.
在像 C++ 这样的一些编程语言中,直接存储和间接引用类型都可以实现上述四种语义中的任何一种。在 .net 和 C# 中,具有公开字段的直接存储类型始终实现 #3,具有公开字段的类类型始终实现 #4,其他值类型可以实现上述任何一个,其他引用类型可以实现 #1、#2,或#4,但不是#3,只有轻微泄漏的抽象。字符串实现#1。
回答by hadi teo
Maybe the article Strings in .NET and C#can help you. According to this article, string is a reference type.
也许.NET 和 C# 中的字符串一文可以帮助您。根据这篇文章,字符串是一种引用类型。

