Reference Types and Value Types

  • Types in the .NET Framework come in two varieties: value type and reference types. The primary difference between these two types is to do with the way variable data is accessed. Let us see how it is done.
  • Application data memory is divided into two primary components, the stack and the heap.
  • The stack is an area of memory reserved by the application to run the program.
  • All the data associated with a value type is allocated on the stack. When a variable of a value type goes out of scope, it is destroyed and its memory is reclaimed
  • A variable of reference type on the other hand, exists in two memory locations. The actual object data is allocated on the heap.
  • A variable containing a pointer to that object is allocated on the stack
  • When that variable is called by a function, it returns the memory address for the object to which it refers. When that variable goes out of scope, the object reference is destroyed but the object itself is not.
  • If any other references to that object exists, the object remains intact. If the object is left without any references, it is subject to garbage collection

Examples of value type:
  • Integer, Boolean, Char etc. and user defined types such as Structures and Enumeration.
  • In VB.NET, you use the Dim statement to create a variable that represents a value type.
  • Dim I As Integer
  • The above line tells the runtime to allocate the appropriate amount of memory to hold an integer variable. Although this line creates a variable, it does not assign a value to it.
  • You can assign a value using the assignment operator
  • I = 55

  • You can also choose to assign a value to a variable upon creation, as shown below
  • Dim I As Integer = 55


Examples of reference type:
  • Classes, Interface, Delegate, Array etc.
  • Creating an instance of a type is a two-step process. The first step is to declare the variable as that type, which allocates appropriate amount of memory for that variable but does not actually create the object. The following syntax declares an object
  • Dim myForm As System.Windows.Forms.Form

  • The above line tells the runtime to set aside enough memory to hold a Form variable and assigns it the name myForm, but it does not actually create the Form object in memory.
  • The second step called instantiation, actually creates the object
  • myForm=New System.Windows.Forms.Form( )
  • The above line makes a call to the constructor method of the type
  • System.Windows.Forms.Form by way of New keyword.

  • If desired, you can also combine both declaration and instantiation into a single statement as shown in the following example
  • Dim myForm As New System.Windows.Forms.Form( )

  • By declaring and instantiating an object in the same line, you reserve the memory for the object and immediately create the object that resides in the memory.
  • Imports System.Windows.Forms.Form
  • just an example to show creating the objects, reference type demo,value type demo etc.

Public Class Form1
Dim myform As System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim x, y As New System.Windows.Forms.Form()
x.Text = "This is form 1"
MessageBox.Show(x.Text)
y = x
MessageBox.Show(y.Text)
y.Text = "This is form 2"
MessageBox.Show(y.Text)
a = New class1()
a.strn = "hello object a"
MessageBox.Show(a.strn)
myform = New System.Windows.Forms.Form()
myform.Text = "Hello myform"
MessageBox.Show(myform.Text)
Dim m, n As Integer
m = 15
MessageBox.Show(m)
n = m
MessageBox.Show(n)
m = 25
MessageBox.Show(m)
End Sub
Public a As class1
Public Class class1
Public strn As String
End Class


The .NET Base Class Library << Previous

Next >> What is Classes?

Our aim is to provide information to the knowledge


comments powered by Disqus




Footer1