c# - 引用类型的堆内存引用存储在哪里?

标签 c# .net reference-type

简单代码例如:

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/

相关文章:

c# - MVVM 事件处理程序属于哪里

c# - 这安全吗,还是只能在英语国家使用?

c# - System.Text.Encoding.GetEncoding ("iso-8859-1") 抛出 PlatformNotSupportedException?

javascript - 返回长度为 5 的空白数组

C# 列表在添加元素或创建副本后是否保存对元素的引用?

c# - 从 C# 在 Oracle 数据库中输入日期参数​​的正确格式是什么

c# - Visual Studio 2015 无法识别 svg、rect、g、circle 等

c# - WCF 故障异常

asp.net - 如果 App_Data 不存在,如何让 msdeploy 创建它,但不删除远程目录的任何内容?

java - 使用对象变量引用新的数据类型/对象