top of page

2_A

  • hrafnulf13
  • Oct 1, 2020
  • 1 min read

Updated: Oct 11, 2020


The Types


The types in .NET Framework can be of value type or reference type [1]. A value type holds the data within its own memory allocation and a reference type contains a pointer to another memory location that holds the real data. Reference type variables are stored in the heap while value type variables are stored in the stack.


Memory allocation [1]:


 

Value Types

A value type stores its contents in memory allocated on the stack [1-4]. When a value type is created, a single space in memory is allocated to store the value and that variable directly holds a value. When assigning it to another variable, the value is copied directly and both variables work independently.


The following data types are all of value type [1-4]:

  • bool

  • byte

  • char

  • decimal

  • double

  • enum

  • float

  • int

  • long

  • sbyte

  • short

  • struct

  • uint

  • ulong

  • ushort

 

Reference Types

Reference types are used by a reference which holds a reference (address) to the object but not the object itself [1-4]. Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another doesn't copy the data. Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value. Reference type variables are stored in a different area of memory called the heap. This means that when a reference type variable is no longer used, it can be marked for garbage collection.


The followings are reference type data types [1-4]:

  • String

  • Arrays (even if their elements are value types)

  • Class

  • Delegate

 

Example

C#

Link:


Form.cs


Output


Video


VB.net

Link:


Form.vb


Output


Video



References




Recent Posts

See All

Comments


bottom of page