Thursday, October 31, 2013
Value Types and Reference Types in .Net
Data Types in the programming scenario are the categorized sections,which are based on the type of data stored in them.Generally different types of data are stored in different memory with different structures such as stack or heap memories.
Some data types holds its data within its own memory allocation and some store only the address of the data[Data is present at some other location].So Data Types are categorized based on the way how they store their values .
Generally we come across many data types in our programming journey like int, char, float,object etc. But to be clear all the Data Types are classified into two sections , they are:
- Value Type
- Reference Type
- The Data Types which store their values directly in their stack memory are known as Value Types.
- Among the built-in Data Types ,except 'String' and 'Object' all other data types are considered value types
- Among user defined data types 'structure' and 'enum' are taken as value types.
- Examples: All numeric data types, char bool, date, structures and enum etc.
- Reference Types are those which store their values in heap memory and the address of those values are stored in stack memory as variables.
- Among the built-in data types 'string' and 'object' are taken as Reference Types.
- Among user defined data types Arrays, Class, Interface, and Delegate etc are taken as reference types.
- Examples:string, object, arrays, delegates, interface etc.
Hope it helps you, Please feel free to suggest and post your valuable comments.