How the value types are stored in C#? Explain


Value types in the C# are stored in the stack if they are declared inside the local method. If Value types are declared inside a class (reference type) then compiler allocates memory in Heap.

Memory allocation for value type and reference type

Generally, it completely depends on JIT compiler how to allocate memory to variables. For the optimization purpose JIT compiler sometimes stores local which are involved in mathematical operations inside registers, but we can't guarantee that if there are not sufficient registers are available then Stack is used.

Share this thread


comments powered by Disqus


Social Media