difference between By Value type and By Reference type

Value types are derived from System.ValueType and stored in Stack memory. References types are stored in garbage collected heap memory.

 

By assigning one value type to another value type, copy of the field data is copied. By assigning one reference type to another reference type, only reference is assigned instead of value.

Example of value type is Structure and example of reference type is Class. Here I will explain each with example.

 

By Value type:

Here we will create some structure, for example Point as shown below.

 

struct Point
{
        public int x;
        public int y;

        public Point(int XPos, int YPos)
        {
            x = XPos;
            y = YPos;
        }
        public void Display()
        {
            Console.WriteLine("x = {0} ; y = {1}", x, y);          
        }
}

 

Structure Point has parameterized constructor which takes two integer values and it displays these values through the Display() method.

Create object for Point structure and copy this object to another object objPoint2 as shown below.

 

static void Main(string[] args)
{
            Point objPoint1 = new Point(20, 30);
           
            //assigning objPoint1 to objPoint2
            Point objPoint2 = objPoint1;

            //Changing the objPoint1 x value
            objPoint1.x = 60;

            Console.WriteLine("By Value Type");
            objPoint1.Display();
            objPoint2.Display();

            Console.ReadLine();
}

 

Here we created the object objPoint1 for Point structure by passing the values 20, 30. Then we copied this object to objPoint2. Now we changed the objPoint1 object x value. If we call the Display method for both objects, we will get output as shown below.

 

 

 

If you observe the output, even though we changed the objPoint1 object x value, the output of objPoint2 object is not changed. That means when we copied objPoint1 object to objPoint2 object, from objPoint1 total data is moved to objPoint2. Because of that then after even we changed the objPoint1 data, it is not affecting objPoint2 data.

 

By Reference Type:

Here we will take same above example but instead of structure, we will consider Class which is reference type.

 

class Point
{
        public int x;
        public int y;

        public Point(int XPos, int YPos)
        {
            x = XPos;
            y = YPos;
        }
        public void Display()
        {
            Console.WriteLine("x = {0} ; y = {1}", x, y);          
        }
}

 

Class Point has parameterized constructor which takes two integer values and it displays these values through the Display() method.

Create object for Point class and copy this object to another object objPoint2 as shown below.

 

static void Main(string[] args)
{
            Point objPoint1 = new Point(20, 30);
           
            //assigning objPoint1 to objPoint2
            Point objPoint2 = objPoint1;

            //Changing the objPoint1 x value
            objPoint1.x = 60;
   
        Console.WriteLine("By Reference Type");
            objPoint1.Display();
            objPoint2.Display();

            Console.ReadLine();
}

 

Here we created the object objPoint1 for Point structure by passing the values 20, 30. Then we copied this object to objPoint2. Now we changed the objPoint1 object x value. If we call the Display method for both objects, we will get output as shown below.

 

 

If you observe the output, whenever we changed the objPoint1 object x value, the output of objPoint2 object also changed. That means when we copied objPoint1 object to objPoint2 object by reference, from objPoint1 only reference is moved to objPoint2. Because of that then after even we changed the objPoint1 data, it is affecting objPoint2 data.

 

From the above discussion we observed that, when we copied through by value total data is moved and through by reference only reference is moved.

                                                                

                                                                                                    ByValueTypeExample.zip (21.76 kb)

                                                                                                       ByRefTypeExample.zip (21.46 kb)