Top 5 Difference between Call by Value and Call by Reference

In this post, we’ll look at a simple but difficult interview question in C#: Call By Value vs Call By Reference. This is a question that both fresher and experienced developers are asked in interviews. It’s one of the most frequently asked questions by technical interviewers.

Call by Value and Call by Reference
Difference between Call by Value and Call by Reference

There are two ways to call a method or a function in programming languages: Call by Value and Call by Reference.
It’s important to remember that in C#, all objects are passed by value by default, not by reference, regardless of whether they’re Value Types or Reference Types.

What is call by Reference in C#?

In the call-by-reference mechanism, the address or reference of a variable is passed to a function parameter rather than a copy of the actual value. As a result, any changes to passed values are permanent and affect the value of the original variable.
To specify a reference type parameter you must use the `ref` Keyword at the time of parameter declaration as well as in the calling method. This ref keyword is responsible for passing the function a reference to the arguments rather than a copy of the actual value.

C# Pass By Reference declaration:

We have added the ref keyword to the input parameter in the method IncrementValue, indicating that the argument should be passed by reference when calling this method:

 public static void IncrementValue(ref int i)
   {
            i++;
   }

Now we’ll use the above method where parameters are supposed to be passed to the function by reference using the ref keyword at the caller side. If the ref keyword is not used on the caller side, a compilation error will occur as result.

  static void Main(string[] args)
        {
            int x = 100; // The variable must be initialized first in the case of a ref.
            IncrementValue(ref x);
            Console.WriteLine(x);
            // Output: 101
        }

If you look at the above result, you’ll notice that the changes we made to the variable x in the called method are also reflected in the calling method.

Note: In order to use the ref argument in a c# program, the ref keyword must be used explicitly in both the method declaration and the calling method.

What is call by Value in C#?

C# Call by value: In this case when we call the method of any class (which takes some parameter) from the main method using the object. Then the value of the parameter in the main method will be directly copied to the class method to parameter values respectively. In this case, if some changes occur in values within the method that change does not occur in the actual variable.

In call-by-value, the functions are free to make changes to parameter values without any risk of impact on the original variable.

Note: The Value-Type variables will store the value of the variable directly in memory, whereas Reference-Type variables will save a reference to the data.

C# Call By Value Example

using System;
namespace CallByValueExample
{
    class Program
    {
        // Pass by value example
        public static void IncrementValue(int i)
        {
            i ++;
        }
        static void Main(string[] args)
        {
            int x = 100; 
            IncrementValue(x);
            Console.WriteLine(x);
            Console.ReadKey();
            // Output: 100
        }
    }
}

As shown in the above result, the changes made inside of the method have not affected the original value of the variable.

Call by Value VS Call by Reference

Call By ValueCall By Reference
1. In the case of call by value, the copy of the variable is directly passed to the method.In the case of call-by-reference, the variable itself or the reference address of that variable is passed.
2. In this method, the value of a provided parameter is copied into the argument of the function. As a result, any modifications made to any of the function’s arguments have no impact on the provided parameter.In this method, the argument and the given parameter both point to the same address. As a result, any changes made to any of the function’s arguments are reflected in the passed parameter.
3. The original value is not modified in any way.The original value has been changed.
4. It won’t let you change the actual variables using function calls.It allows you to alter the values of actual variables by using function calls.
5. The memory location is referred to both for passed parameters and the actual arguments of a function are different.Memory location referred by both passed parameter and actual argument of the function is same.
6. It doesn’t require a ref or out keyword. It requires a ref or out keyword to achieve call-by-reference.
Call-by-value-vs-call-by-reference

Note: If you want to use pass-by-reference, you must use the out or ref keyword, regardless of whether the argument type is a value type or a reference type. In this situation, because the parameter utilizes the same storage location as the argument any changes to the parameter inside the method will reflect the original variable.

FAQ

What is the meaning of the term “call by value” in C#?

The call-by-value method copies the value of variables into the function’s formal parameter. In this case, any changes to the parameter inside the function have no effect on the argument or actual variable’s value outside the function.

Can you explain what the term “call by reference” means in C#?

When passing arguments to a function via reference, the actual address of an argument is copied into the formal parameter. The address is used within the function to obtain the actual parameter used in the call. It signifies that any changes to the parameter have an impact on the passed argument or the variable’s actual value outside of the function.

What is the difference between call by value and call by reference?

The major difference between Call By Value and Call By Reference is that in the call by reference, the address or reference of an argument is copied to the function’s formal parameter, whereas in the call by value, the separate copies of the variable’s value are passed to the formal parameter of the function.

References: MSDN-Passing Parameters (C# Programming Guide) | Call by Value and Call by Reference

Thank you for taking the time to read the blog; if you found it useful, please leave a comment and share it with others.

Articles to Check Out:

c20e93cf99b4a0eb1e4a099de6c2c300?s=250&r=g
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments