Passing by Value vs. Passing by Reference 

In earlier post we have seen how value type and reference type variable instances behave differently when create copied variable and changing copied variable values.

Pass by value

Pass by value means you are making a copy in memory of the actual parameter’s value that is passed in, a copy of the contents of the actual parameter. Use pass by value when you are only “using” the parameter for some computation, not changing it for the program the modification done to the parameter passed does not reflect in the caller’s scope while in the call by reference, the modification done to the parameter passed are persistent and changes are reflected in the caller’s scope.

By default, all method we create for value type is Pass by value. For e.g. in below program value changed in respective method does not get used in the main program. Scope of variable changes is limited only to method.

using System;
namespace General{
    class Program
    {
        static void Main()
        {
            decimal price = 20;            
            Console.WriteLine($"Product Price is {price}");
            Console.WriteLine("applying tax");
            State_GST(price);
            Console.WriteLine($"after applying SGST price {price}");
            Central_GST(price);
            Console.WriteLine($"after applying CGST price {price}");
            Console.WriteLine($"Final product price is {price}");
            Console.Read();
        }
            
        static void State_GST(decimal price)
        {
            decimal tax = price * 2 / 100;
            price += tax;
            Console.WriteLine($"after applying SGST price {price}");
        }
        
        static void Central_GST(decimal price)
        {
            decimal tax = price * 2 / 100;
            price += tax;
            Console.WriteLine($"after applying CGST price {price}");
        }
    }
    
}

Output of above program

Pass by value

Passing by Reference:

Passing by reference means that the memory address of the variable (a pointer to the memory location) is passed to the function. So, if we make any change in any of the variable it will reflect on variable after the function call completed.

In C#, “ref” keyword uses to pass the value by reference.

using System;
namespace General{
    class Program
    {
        static void Main()
        {
            decimal price = 20;
            Console.WriteLine($"Product Price is {price}");
            Console.WriteLine("applying tax");
            State_GST(ref price);
            Console.WriteLine($"after applying SGST price {price}");
            Central_GST(ref price);
            Console.WriteLine($"after applying CGST price {price}");
            Console.WriteLine($"Final product price is {price}");
            Console.Read();
        }
            
        static void State_GST(ref decimal price)
        {
            decimal tax = price * 2 / 100;
            price += tax;
        }
                
        static void Central_GST(ref decimal price)
        { 
            decimal tax = price * 2 / 100;
            price += tax;
        }
    }
    
}

Output of above program

When should consider pass-by-reference over pass-by-value:

  • If you are calling a function that needs to modify its arguments.
  • If you’re calling a function that needs to take a large object as a parameter.
  • If you’re writing a copy or move constructor which by definition must take a reference.
  • If you’re writing a function that wants to operate on a polymorphic class.
  • If you’re writing a function that might return a very large or uncopiable object.

Passing by value and passing by reference choice is applicable only with value type.

Reference type variable by default get pass as by reference (string exceptional case)

For e.g. if we are passing Array input parameter to method then its explicitly array parameter gets passed as reference.

Leave a Reply

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