Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1> value types are stored in the stack .
2> objects are stored on the heap.
3> So the value types inside the reference types are also stored in heap.
4> But we cant declare value types independently i.e we have to wrap them up in a class and create objects to access them.
5> in this way the 1st statement is contradicted.

Please help me as i am getting different things in different articles, so cant conclude.

What I have tried:

I have looked into different articles but not satisfied as i am getting different and contradicting responses.
Posted
Updated 30-Mar-17 7:48am

Quote:
value types are stored in the stack .


static int x; // is this on the stack? :)


Objects are stored in the heap and all properties in that object (even value types) are a part of that object so they are also on the heap.
 
Share this answer
 
Comments
Sushant Prusty 29-Mar-17 11:31am    
Thanks sir.
Here x is a static variable and when it is allocated, it will be stored as part of Methodtable in Appdomain. So when the class is loaded for the first time in the application, separate memory will be allocated in appdomain for class level variables and methods inside it.
If static variable is primitive type, it will be stored as part of Methodtable. If it is reference type then it will be stored inside the heap and reference is stored in Methodtable.
So what is stored inside a stack or register?
The "stack" and "heap" are implementation details, and not as important as the observable characteristics of value and reference types:
The Stack Is An Implementation Detail, Part One – Fabulous Adventures In Coding[^]
The Stack Is An Implementation Detail, Part Two – Fabulous Adventures In Coding[^]

But the terms can still be useful when thinking about how the types behave.

I think it would be more correct to say that the data for a value type is stored inline, whereas the data for a reference type is stored elsewhere (on the heap), with the address of that data being stored inline.

So, given a reference type containing two fields:
C#
class Foo
{
    int x = 42;
    string s = "Bar";
}

and an instance of that type:
C#
Foo f = new Foo();

f stores the address of the data for that instance - eg: 0x1234567. That data might look something like:
// i:
0x1234567:   0
0x1234568:   0
0x1234569:   0
0x1234570:  42

// Address of s:
0x1234571:  11
0x1234572: 173
0x1234573: 240
0x1234574:  13

The data for i is stored inline: 42. But the data for s is stored somewhere else, at address 0x0BADF00D:
0x0BADF009:   0 // Length
0x0BADF00A:   0 
0x0BADF00B:   0 
0x0BADF00C:   3 
0x0BADF00D:  66 // "B"
0x0BADF00E:  97 // "a"
0x0BADF00F: 114 // "r"

Of course, this ignores the concept of "boxed" value types[^], which are stored in the same way as reference types.

Some more light reading: :)
The Truth About Value Types – Fabulous Adventures In Coding[^]
Debunking another myth about value types – Fabulous Adventures In Coding[^]
Six important .NET concepts: Stack, heap, value types, reference types, boxing, and unboxing[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900