Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference types live on the heap, value types live on the stack

Tags:

c#

While reading "C# in Depth" I was going through the section titled "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
    }
}

Just want to clarify if my assumptions are right. Thanks.

EDIT: I should have used diff variables, as I think what I had initially created confusion. So I have modified the code.

EDIT: Yes, as Jon mentioned - it's a myth. I should have mentioned that. My apologies.

like image 611
Wondering Avatar asked Aug 22 '10 15:08

Wondering


1 Answers

https://docs.microsoft.com/en-us/archive/blogs/ericlippert/the-stack-is-an-implementation-detail-part-one

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.

like image 90
siride Avatar answered Sep 18 '22 13:09

siride