.NET栈和堆,什么去哪里,当我宣布一个字符串?当我、字符串、去哪里、NET

由网友(物是人飞)分享简介:如果我执行此行中我创建一个字符串,它是一个参考。 If I execute this line I create a string which is a reference. string mystring = "Hello World"就是在同样的情况下,我声明它的对象变量 MyString中?而在堆上的数据的H...

如果我执行此行中我创建一个字符串,它是一个参考。

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"

阅读全文

相关推荐

最新文章