Value Type, Reference Type, Object Type - ablealias/asp.net GitHub Wiki

Data Types are implemented based on their classification. Data types can be classified according to whether a variable of a particular type stores its own data or a pointer to the data. If it stores its own data it is a value type; if it holds a pointer to data elsewhere in memory it is a reference type.

Value Types

A data type is a value type if it holds the data within its own memory allocation. Value types include the following:

  • All numeric data types
  • Boolean, Char, and Date
  • All structures, even if their members are reference types
  • Enumerations

**A Value Type stores its contents in memory allocated on the stack. **

Reference Types

A reference type contains a pointer to another memory location that holds the data. Reference types include the following:

  • String
  • All arrays, even if their elements are value types
  • Class types, such as Form
  • Delegates

A class is a reference type. **Reference Type variables are stored in a different area of memory called the heap. **

Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM.

NULL

Reference types have null value by default, when they are not initialized. For example, a string variable (or any other variable of reference type datatype) without a value assigned to it. In this case, it has a null value, meaning it doesn't point to any other memory location, because it has no value yet.

A value type variable cannot be null because it holds a value not a memory address. However, value type variables must be assigned some value before use.

Object Type

Holds addresses that refer to objects. You can assign any reference type (string, array, class, or interface) to an Object variable. An Object variable can also refer to data of any value type (numeric, Boolean, Char, Date, structure, or enumeration).

You can find out whether an Object variable is acting as a reference type or a value type by passing it to the IsReference method in the Information class of the Microsoft.VisualBasic namespace. Information.IsReference returns True if the content of the Object variable represents a reference type.