是字符串值类型或引用类型

时间:2010-08-11 15:04:48

标签: .net asp.net

string是引用类型还是值类型?任何人都可以给出相应的描述吗?

3 个答案:

答案 0 :(得分:8)

string是一个不可变参考类型。以下是一个简短的例子:

// All of these point to the same string in the heap
string hello = "Hello World!"; // creates string
string hello2 = "Hello World!"; // uses the previous string from the intern pool
string hello3 = hello2;

如果您正在寻找更多信息,请查看Jon Skeet的帖子:

C# in Depth: Strings in C# and .NET

答案 1 :(得分:3)

.net框架中的

System.String是一个引用类型,一个非常好的解释是Jon Skeet:C# in Depth: Strings in C# and .NET。他的文章的关键点是:

  • 是参考类型
  • 这是不可改变的
  • 它可以包含空值
  • 它会重载==运算符

最后一点是使string的行为类似于值类型的那一点:

string s1 = "value";
string s2 = "value";
// result will be true.
bool result = (s1 == s2);

答案 2 :(得分:0)

我们自己的主人John Skeet从他的书“C#in Depth”中查阅了Strings in C# and .NET一章。它告诉你所有你需要知道的事情。

相关问题