Types in .Net

For any program, Variables are required to either hold the data, store the data or to accept the data. In .NET, Variables are declared using types. .NET uses the Common Type System (CTS) to define data types.

In .NET, these types are categorized based on how values stores into the memory. There are two types in .Net i.e. value types and reference types. Instances of these types are referred to as values and objects, respectively. Value type instance holds the bytes of data whereas reference types just contain the address of another instance.

Value types:

A value type stores its values or contents in memory allocated on the stack. When we create a value type variable, some memory gets allocated on the stack for the variable, and value gets save into that memory.

For e.g.

int firstVariable = 100;

When the above line gets executed, the program will allocate memory to variable “firstVariable” to store value “100”

In .Net following data types are of value type:

  • bool
  • byte
  • char
  • decimal
  • double
  • enum
  • float
  • int
  • long
  • sbyte
  • short
  • struct
  • uint
  • ulong
  • ushort

Now if we assign the value of value type variable to another instance then another instance will be allocated with separate memory on the stack

For e.g.

int firstVariable = 100;

int secondVariable = firstVariable;

Value type memory allocation
using System;
namespace General{
    class Program
    {
        static void Main()
        {
            var firstVariable = 100;
            Console.WriteLine($" Original instance Value {firstVariable}");
            var secondVariable = firstVariable;
            Console.WriteLine($" Copy instance Value {secondVariable}");
            secondVariable = 200;
            Console.WriteLine(" **After changing the copy instance variable**");
            Console.WriteLine($" Original instance Value {firstVariable}");
            Console.WriteLine($" Copy instance Value {secondVariable}");
            Console.ReadLine();
        }
    }
    
}

Output of above program

Value type program outputs

Reference types

The reference type doesn’t store its value directly. Instead, Reference Types will contain a pointer which points to other memory location that holds the data. The Reference Types won’t store the variable value directly in its memory instead, it will store the memory address of the variable value on Heap to indicate where the value is being stored.

For e.g.

String str = “Hello World”;

the system will store the variable value “Hello World” in one location and the variable ” str” in another location along with the memory address of the variable value.

In .Net following data types are of reference type:

  • String
  • Array
  • Class
  • Delegates

Now if we assign the value of reference type variable to another instance of reference type then another instance will point to the existing instances. So, making any changes in copy instance, the same will automatically be reflected in the original variable.

using System;
namespace General{
    class Program
    {
        static void Main()
        {
            var a = new A { i = 10 };
            Console.WriteLine($" Original instance Value {a.i}");
            var copyA = a;
            Console.WriteLine($" Copy instance Value {copyA.i}");
            copyA.i = 100;
            Console.WriteLine(" **After changing the copy instance variable**");
            Console.WriteLine($" Original instance Value {a.i}");
            Console.WriteLine($" Copy instance Value {copyA.i}");
            var arr = new int[] {1,2};
            Console.WriteLine(" Original instance Value");
            foreach (var i in arr) Console.WriteLine($" {i}");
            var copyArr = arr;
            Console.WriteLine(" Original instance Value");
            foreach (var i in copyArr) Console.WriteLine($" {i}");
            copyArr[0] = 10;
            copyArr[1] = 9;
            Console.WriteLine(" **After changing the copy instance variable**");
            Console.WriteLine(" Original instance Value");
            foreach (var i in arr) Console.WriteLine($" {i}");
            Console.WriteLine(" Original instance Value");
            foreach (var i in copyArr) Console.WriteLine($" {i}");
            Console.ReadLine();
        }
    }
    
        class A
        {
            public int i;
        }
    
}

Outputof above Program

Reference type program outputs

Note: – String is exceptional case (We will discuss more in another post)

Leave a Reply

Your email address will not be published. Required fields are marked *