In C# universe we know of two kind of types: value types and reference types. When dealing with value types, one variable points to one value, two variables can have the same value but changing value of one variable will not change value of second value. Two or more variables of reference types can contain same value, but when you change value of one variable all other variables are changed too. Values of value types are stored on stack (size of heap is 1Mb), and reference types values are stored on heap (size limitation is 2GB on 32bit system, and pretty-much unlimited on 64bit systems). Stack is designed to be fast and revolves around first in last out principle, we store value types inside stack for fast access and when we do not expect object to live long. Heap, looking at it’s size, is designed to hold bigger variables and variables inside heap can survive longer, meaning we can access it for longer periods of time.
So…how will I know which type is value or reference type? It’s really simple actually. By looking at the type’s name.
|Value types||Reference types|
In next post first we will cover classes (our second reference type) and then cover life-cycle of both value types and reference types.