Why does C# (.Net) prefer stack to store value types? What is the primary reason behind this design? Is it because read/write operations to the stack take better advantage o
Stack operation or Heap operation, they both will be same as you are accessing a memory address which are in two different locations.
Value types are small, int, byte etc, they are small in size and they are referenced very frequently in terms of mathematical computations. Since they are very small in size, 4 to 16 bytes max, (you should not use more then 16 bytes in value type for best performance), alloating such small space on heap and deallocating, garbage collection etc would be very costly.
Every method we enter, on an average we define 10 local value types to use it internally, that will be very costly on heap as reference types.
Stack can grow and shrink easily (not the stack size, but the stack portion used for current Method !!) as the valuetypes are just addressed as offset from stack pointer, and their allocation and deallocation is easy as its simple increment and decrememnt on stackpointer by total size of all valuetypes used.
Where else in reference type, each reference object has its own allocation and sizing, plus CLR has to maintain table of objects which is sort of like index of actual pointers in memory in order to avoid buffer overflows. So one object you use (reference type) actually has two storage, one index entry in CLR's reference table, and actual memory space. Thats why its easy and fast to store value types on stack.