简单代码例如:
Object test=new Object();
我知道 test
对象的内存分配到堆中。
引自 MSDN:
Variables of reference types store references to their data (objects)
但我真的不明白这个变量值(对堆数据的引用)的存储位置,堆栈或堆或其他地方?
最佳答案
test
变量存储在堆栈上 - 它保存堆上对象的地址。对象实例存储在堆上。
我建议你阅读.NET Type Fundamentals Jeffrey Richter 的文章:
When an object is allocated from the managed heap, the new operator returns the memory address of the object. You usually store this address in a variable. This is called a reference type variable because the variable does not actually contain the object's bits; instead, the variable refers to the object's bits.
In addition to reference types, the virtual object system supports lightweight types called value types. Value type objects cannot be allocated on the garbage-collected heap, and the variable representing the object does not contain a pointer to an object; the variable contains the object itself. Since the variable contains the object, a pointer does not have to be dereferenced in order to manipulate the object. This, of course, improves performance.
关于c# - 引用类型的堆内存引用存储在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21263090/