引用类型在堆中生活,值类型住在堆栈上 [英] Reference types live on the heap, value types live on the stack
问题描述
而深度C#读书,我经历的一节引用类型在堆中生活,值类型存在于栈中。
while reading "C# in Depth" I was going through the section "Reference types live on the heap, value types live on the stack."
现在我可以理解的是(主要为引用类型):
Now what I could understand is (mainly for ref type) :
class Program
{
int a = 5; // stored in heap
public void Add(int x, int y) // x,y stored in stack
{
int c = x + y; // c stored in stack
}
}
只是想澄清,如果我的假设是正确的。谢谢。
编辑:
我应该用差异变量,我认为它创造了我已经修改了代码confusion.so
Just want to clarify if my assumptions are right. Thanks. I should have used diff variables, i think it created a confusion.so I have modified the code.
编辑:
是的,乔恩提及 - 这是一个神话,我应该提到that.My道歉
Yes, as Jon mentioned - it's a myth, I should have mentioned that.My apologies.
推荐答案
的http://博客.msdn.com / b / ericlippert /存档/ 2009/04/27 /的堆栈 - 是 - 一 - 实现detail.aspx
整个堆上引用类型,在栈中值类型,不仅是看它一个糟糕的方式,但它是错误的了。
The whole "reference types on the heap, value types on the stack" is not only a bad way to look at it, but it's wrong too.
这篇关于引用类型在堆中生活,值类型住在堆栈上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!