Is string a value type or a reference type?
字符串是值类型还是引用类型?
I just can't find a "good" explanation for this...
我找不到一个“好”的解释。
8 个解决方案
#1
89
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 reference is known in advance, even if the size of the string isn't.
它不能是值类型,因为值类型需要堆栈的已知大小等等。作为引用类型,引用的大小是预先知道的,即使字符串的大小不是。
It behaves like 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...
除了内部的StringBuilder,但是当它这样做的时候,你永远不会看到它……
#2
17
String is an immutable reference type.
字符串是不可变引用类型。
See the blog post Immutable types: understand their benefits and use them on immutability.
请参阅博客文章“不可变类型”:了解它们的优点,并在不可变性上使用它们。
#4
11
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 other memory address where the actual value (or values) will be stored, then it is a reference type.
如果,otoh,当您“声明”变量时分配的内存槽将只保存一些其他的内存地址,其中存储实际的值(或值),那么它就是一个引用类型。
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.
对于一个字符串,该字符串在堆上创建,它的地址位于为变量分配的内存槽中,所以它是一个引用类型。
#5
5
String is an immutable reference type which has certain qualities that give it the occasional appearance of being a value type
String是一种不可变引用类型,它具有某些特性,使其偶尔出现为值类型
#6
4
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框架中字符串的别名。
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是一个引用类型,但是相等操作符(==和!=)被定义为比较字符串对象的值,而不是引用。这使得字符串相等性的测试更加直观。例如:
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。
string b = "h";
b += "ello";
#7
3
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。
#8
2
Maybe the article Strings in .NET and C# can help you. According to this article, string is a reference type.
也许。net和c#中的文章字符串可以帮助您。根据本文,string是一个引用类型。
#1
89
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 reference is known in advance, even if the size of the string isn't.
它不能是值类型,因为值类型需要堆栈的已知大小等等。作为引用类型,引用的大小是预先知道的,即使字符串的大小不是。
It behaves like 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...
除了内部的StringBuilder,但是当它这样做的时候,你永远不会看到它……
#2
17
String is an immutable reference type.
字符串是不可变引用类型。
See the blog post Immutable types: understand their benefits and use them on immutability.
请参阅博客文章“不可变类型”:了解它们的优点,并在不可变性上使用它们。
#3
#4
11
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 other memory address where the actual value (or values) will be stored, then it is a reference type.
如果,otoh,当您“声明”变量时分配的内存槽将只保存一些其他的内存地址,其中存储实际的值(或值),那么它就是一个引用类型。
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.
对于一个字符串,该字符串在堆上创建,它的地址位于为变量分配的内存槽中,所以它是一个引用类型。
#5
5
String is an immutable reference type which has certain qualities that give it the occasional appearance of being a value type
String是一种不可变引用类型,它具有某些特性,使其偶尔出现为值类型
#6
4
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框架中字符串的别名。
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是一个引用类型,但是相等操作符(==和!=)被定义为比较字符串对象的值,而不是引用。这使得字符串相等性的测试更加直观。例如:
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。
string b = "h";
b += "ello";
#7
3
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。
#8
2
Maybe the article Strings in .NET and C# can help you. According to this article, string is a reference type.
也许。net和c#中的文章字符串可以帮助您。根据本文,string是一个引用类型。