Value type in Stack???
-
according to my current knowledge in .net memory management the value types are stored in stack and reference types are stored in managed heap. Form this, its clear that the object of a class will be saved in heap. Then where will be the memory allocated for the value type member of that class??
My small attempt...
-
according to my current knowledge in .net memory management the value types are stored in stack and reference types are stored in managed heap. Form this, its clear that the object of a class will be saved in heap. Then where will be the memory allocated for the value type member of that class??
My small attempt...
-
according to my current knowledge in .net memory management the value types are stored in stack and reference types are stored in managed heap. Form this, its clear that the object of a class will be saved in heap. Then where will be the memory allocated for the value type member of that class??
My small attempt...
For a method's local variables, value types are indeed stored on the stack and reference types on the heap. Reference types are a little confusing, however, because they are really stored in two parts: the reference, which is stored on the stack just like a value type, and the 'body', which is stored on the heap. Now, lets look at the body of a type (reference type or value type). Reference types and value types can have fields of both types. The fields of a type are (effectively) concatenated together. So the body of a type is the concatenation of the contained value types and the references to the contained reference types. And this body is 'a chunk of memory' which is stored somewhere - on the stack (if its the body of a value type), on the heap (if its the body of a reference type) .. or nestled inside the body of some other type (if its a field in another type). So, it may be more useful to think of value types as being stored 'where they are declared' instead of thinking of them as being stored on the stack, while reference types have their reference stored where the variable is declared, and its body is always stored on the heap.