Value Type :
- When a value-type instance is created, a single space in memory is allocated to store the value.
- Primitive types such as integer, float, Boolean and char are also value types.
- Structure are value types.
- Memory Allocation : Value type local variable stored in Stack.
- C# parameters are (by default) passed by value.
Reference Type :
- With reference types, however, an object is created in memory, and then handled through a separate reference – rather like a pointer.
- Class is Reference types.
- Memory Allocation : The reference portion of reference type local variable store in Stack and content of reference type object store in Heap.
- In the first case , one space of memory is allocated of p1 whereas in the second case, two spaces are allocated: one for a Form object and another for its reference (f1).
Here’s what it now looks like in memory: