Value Type and Reference Type in C#

 

In .Net all data types are majorly divided into two groups Value Type and Reference Type. In Value Type of variables exact data will store whereas in Reference Type of variables only reference will store. Value types are stored in Stack memory whereas Reference types are stored in Heap memory.

 

Examples of Value type are Structures, Enumerations, int, Int32, Int64, decimal, float, bool....etc. Examples of Reference types are Class, Interface, delegate...etc. String is actually reference type where data stored in heap memory but behaves like value type which means if you assign one string variable value to another variable data will be copied instead of only reference. In this article discuss about Value Type and Reference Type.

 

Value Type:

 

As we discussed value type variables store actual data and in stack memory. We will test this behaviour by taking two value type variables (for example int type) and assign one variable data to another as shown below.

           

            int i1 = 10;

            int i2 = i1;

 

            Console.WriteLine("Before i1 Value change: i1 = {0} && i2 = {1}", i1, i2);

            i1 = 100;

            Console.WriteLine("After i1 Value change: i1 = {0} && i2 = {1}", i1, i2);

            Console.ReadLine();

 

Output:

                            

 

As shown above we assign the i1 value to i2, so first i1 and i2 values are 10, 10. Even though after changing the i1 value to 100, i2 value won't change  still i2 value is 10 only. This proves the value type of variables contains data only not reference.

 

Reference Type:

 

Reference types contain only reference of data and data stored in heap memory. As we know class is a reference type, we will test reference type behaviour with the Employee Class example as shown below.

 

Employee.cs

 

using System;

namespace ValueReferenceTypeExp

{

    public class Employee

    {

        public int iId;

 

        public Employee(int iEmpId)

        {

            iId = iEmpId;

        }

 

        public void Display()

        {

            Console.WriteLine("Id = {0}", iId);

        }

    }

}

 

Program.cs

 

using System;

 

namespace ValueReferenceTypeExp

{

    class Program

    {

        static void Main(string[] args)

        {

            Employee obj1 = new Employee(10);

 

            //assign obj1 to new Employee type

            Employee obj2 = obj1;

 

            obj1.iId = 20;

 

            obj1.Display();

            obj2.Display();

 

            Console.ReadLine();         

        }

    }

}

 

Output:

 

                   

 

As shown above we have Employee class with parameterized constructor which expects some integer data. We created one object obj1 by passing 10 as value for iEmpId and we assign the obj1 to obj2 which is also Employee type. Now change the obj1 iId value to 20 and call the Display() method for both objects obj1 and obj2. Both will display output as 20 only even though we change only obj1 value, this is because while assigning obj1 to obj2 only reference is assigning not data. So if you change the obj1 data, it is effecting for obj2 object also.

                                                                                                       

                                                                                                       ValueReferenceTypeExp.zip (23.33 kb)

Add comment