Value Type and Reference Type variables

There are two types of memory available on disk while storing the variables that we declare - 1. Stack memory and Heap memory.

Value Type variables
1. Value type variables gets stored on Stack and stores data in its own memory location, i.e. both variable and data gets stored on stack.

2. When we copy data of one value type variable to another value type variable then 2 different copies of memory location gets assigned to these 2 variables.

3. All data types mentioned in our Data Type in C# section are of value type except string.

4. All structures that we declare in our program are value type even though its members declared are reference types. If you see int data type, it is a structure.

Reference Type variables
1. Reference type variables that we creates also gets stored on stack but the data associated with it gets stored on Heap, i.e. The variable that we declare contains reference of address for memory location of the data which is stored on heap and the variable points to that memory location.

2. Data type such as String, Array, classes, Interfaces, Delegates are all of reference types.

3. When we copy data of one reference type variable into another reference type variable then 2 different copies of these variables gets created on stack, but both these variables contains reference to same memory location on heap where the data gets stored.

Lets take a look at example below.

+
using System;
 
namespace examples
{
    class Program
    {
        static void Main(string[] args)
        {
            //value type variables
            int i;
            i = 3;
            Console.WriteLine("i = " + i);
 
            int j = i;
            Console.WriteLine("j = " + j);
 
            j = 4;
            Console.WriteLine("i = {0}, j= {1}", i, j);
 
            //reference type variables 
            class1 cls1 = new class1();
            cls1.val = 1;
 
            class1 cls2 = cls1;
 
            Console.WriteLine("cls1.val = " + cls1.val);
            Console.WriteLine("cls2.val = " + cls2.val);
 
            cls2.val = 3;
 
            //After changing data
            Console.WriteLine("cls1.val = " + cls1.val);
            Console.WriteLine("cls2.val = " + cls2.val);
 
            Console.ReadLine();
        }
    }
 
    //created class for use in reference type example
    class class1
    {
        public int val;
    }
}

If you look at the above code we have declared 2 int variables i and j. When I copy data from i to j, separate copy of data for j gets created on stack. in next line i am changing the value of j. when we print this on console, i value doesn't get changed. So this concludes that there is separate copy of data for variable i and j.

In contrast to this, in above code When I declare variable cls1 of type class1(class is reference type), assign value as 1, Memory gets allocated for this cls1 variable on stack which contains reference for address of memory location for data i.e. 1 which gets stored on Heap.
Now when I create another variable of class1 cls2, and copy data of cls1 into it, memory gets allocated for this variable on stack but the it contains same reference of address for memory location for the data to which cls1 pointing i.e. 1. No separate copy of data gets created in this case for cls2 variable.
Now see next line where I assigned new value to cls2 i.e. 3 and I have not made any change in cls1 value. If you check the output, you can see that value for cls1 also gets changed to 3. This is because cls1 and cls2, both pointing to same memory location on Heap.

Value type and Rereference type variable in C#

No comments :