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.
https://learn.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.