Value Types versus Reference Types in ASP.NET and C#
Value types are types store inside the stack. Stack is much faster part of the memory and simplistic types are stored there. In contrary, hip is slower and stores more complex types such as reference types. Most of the time it is classes. One good example is String class which is reference type and not a value type. Int, Bool, Double are value types. Structures are also value type and always declared as struct.
Value types can be of several types:
- Built-in types
- User-defined types
- Enumerations
Nullable<bool> myVariable = null;
Reference Types store their references to objects in a stack but actual reference is pointing to is stored in the hip. The idea behind this arrangement is such that by leaving reference in the stack we can easily remove it from there and then garbage collection can take care of all left over objects which were pointed by the reference and which now lost this reference from the stack. This is easy way to clear heap.