.NET栈和堆,什么去哪里,当我宣布一个字符串? [英] .NET stack and heap, what goes where when I declare a string?
问题描述
如果我执行此行中我创建一个字符串,它是一个参考。
If I execute this line I create a string which is a reference.
string mystring = "Hello World"
就是在同样的情况下,我声明它的对象变量 MyString中
?而在堆上的数据的Hello World
?
Is variable mystring
in the same context as the object I declare it? And the data "Hello World"
on the heap?
推荐答案
官方的回答:这是一个实现细节,你不需要知道。
Official Answer: it's an implementation detail and you don't need to know.
有当然可知针对特定(当前)版本的框架,
It is of course knowable for a specific (current) version of the framework,
1),如果这是一个方法里面,那么 MyString中
是在栈上的参考变量。
1) if this is inside a method, then mystring
is a reference variable on the stack.
2),如果这是一个类里面的,那么每个实例将包含 MyString中
字段,在堆上。
2) if this is inside a class, then each instance will contain a mystring
field, on the heap.
在这两种情况下,有含的Hello World堆一个匿名字符串对象
In both cases there is an anonymous string object on the heap containing "Hello World"
这篇关于.NET栈和堆,什么去哪里,当我宣布一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!