Call by value and Call by reference in C#

By
Parameter-we can pass two type of parameter to the method.
  1. Input parameter
  2. Out parameter
  • Input parameter- This kind of parameter is specify to give the same input to method   for calling it.
There  are two type of parameter 
  1.  Call by value
  2. Call by reference
  • Call by value  -> In this case when we call the method of any class (which takes some parameter) from main method using object.Then value of parameter in main method will directly copy to the class method to parameter values respectively. In this   case if some changes occurs in values within the method that change not occurs in actual variable .I have full describe this concept through programming which is given below.
  • Call by reference -> In this case when we call the method,the reference address of variable is passed to the method.If some changes occurs in values within the method that changes occurs in actual variable.To specify this parameter we use 'ref' Keyword at the time of parameter declaration as well as the calling method.
    I have  full explained these two concept through programming which is given below.There are some steps please follow it 

Step 1 - First open your visual studio->File->New->Project->console Application->OK->write the program which is below:
see it

using System;
namespace callbyvalue
{
    class Program
    {
        public class employee
        {
            public void display(int a, String b)
            {  
                Console.WriteLine("Integer value is"+" " +a);
                Console.WriteLine(" String value is" + " " + b);
                Console.ReadLine();
            }

        }
        public class student
        {
            public void show(ref String str)
            {
                Console.WriteLine("Enter the value");
                string s = Console.ReadLine();
                str = str + s;
                Console.WriteLine("value in str variable is"+" "+str);
                Console.ReadLine();
            }
            
        }
        //all class member is called through main method.

        static void Main(string[] args)
        { 
            //creating the object of employee class first and implementing the call by value concept.
            String m = "sunil";
            employee emp = new employee();
            emp.display(200,m);
            Console.WriteLine("value in variable m is" +" "+m);
            Console.ReadLine();

          //creating the object of employee class first and implementing the call by Reference concept 
            string msg="Hello";
            student st = new student();
            st.show(ref msg);
            Console.WriteLine("value in msg is" +" "+msg);//value at address msg will  be print,because here address is copy not value thatswhy at same address value will be print 
            Console.ReadLine();
        }
    }
}
    Step 2 - Now Run the program(press F5).
    Output:

    Description:-There are some steps to describe the whole program.
    • First i have created a employee class ,In employee class i have taken a display method which takes two parameter.Ex display(int a, String b).On this class i have implemented call by value concept.
    • After that i have created a student class .In student class i have taken a show method which takes one parameter with ref keyword.This class is used for implementing the call by reference concept in c#.
    • After that i have created object of employee class in main method,here i have define a variable m which holds the sunil value .after that i called display method using  object.EX.  emp.display(200,m), these value directly pass to the display method. see it. a <--200, b <--m<--sunil and  display method will print a and b values,if we print m value then sunil will print because here value is copy not reference address.
    • After that i have created object of student class  in main method and called student class method by the object.Ex  st.show(ref msg);here reference address of msg will copy to the str variable.If we print msg value in main method then different value will print which is calculated by the show method in student class.
    • In call value actual value is copy to the method but In call by reference  reference address of value is copy to the method.
    Difference between value type and reference type:
    There are following difference between value type and reference type on the basis of storage.
    1. Value type store within stack memory and reference type store within heap memory.
    2. Structure,all primitive data type except string,enum are the example of value type.Class,string,array,delegate,interface is the example of reference type.
    3. Value type directly store values within stack but reference type contains reference object of value which store in heap.
    4. when value type is copy to another value type then actual value is copy,but when reference type is copy to another reference type then reference address of value is copy .
    5. Value type can be initialize with zero(0) and reference type initialize with NULL.
    I hope this helpful for you.if any problem please comment it, i will improve it as soon as possible.
    To Get the Latest  Free Updates Subscribe
    Click below to download whole application. 
               Download

    7 comments:

    Powered by Blogger.