C# | Where Value types and Reference Types are Stored?

Spread the love

Reference types are always stored on Heap. But value types are stored on based on where they are declared.

  1. Value types declared as class members will be allocated on Heap.
  2. Value types declared in function arguments, inside function are allocated on Stack.
  3. Value type in struct will depend on where the struct has been created. If as class variable, then on heap else on stack.
  4. If using ref keyword for struct declaration, then it will always be on stack, and because of that it will have a limitation, that it will never be created as a class property.