Value Types and Reference Types in .Net
A data type is a value type if it holds the data within its own memory allocation. A reference type contains a pointer to another memory location that holds the data.
All the data types in .net are classified in to value types and reference types.
- The data types whose values are directly stored in stack memory area are called as value types and the data types whose values are stored in heap memory area and its address is stored in a variable in stack memory area are called as reference types.
- Among all built in data types of .net string and object are reference type and all other data types are value types.
- Among user defined data types, class, interface, delegate and arrays are reference type while structure and enumeration are value type.
Value types include the following:
- All numeric data types
- Boolean, Char, and Date
- All structures, even if their members are reference types
- Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong
Reference types include the following:
- All arrays, even if their elements are value types
- Class types