Passing Arguments by Reference

[print-me]

Arguments can be passed by reference. That simply means that we are passing the address of the variable and not its value. Pass by reference is useful when you are passing large-sized arguments such as objects. When we modify or use the passed argument inside the method, the originating source or the variable that we are referring outside the method is modified as well. The following is the basic syntax of defining parameters that will accept address instead of values.

returnType MethodName(ref datatype param1)
{
   code to execute;
}

You noticed that we used the ref keyword. When calling the method and passing the argument, we use the ref keyword as well.

MethodName(ref argument);

Let’s demonstrate the difference of passing an argument by value and passing it by reference.

using System;

namespace PassByReferenceDemo
{
    public class Program
    {
        static void ModifyNumberVal(int number)
        {
            number += 10;
            Console.WriteLine("Value of number inside method is {0}.", number);
        }

        static void ModifyNumberRef(ref int number)
        {
            number += 10;
            Console.WriteLine("Value of number inside method is {0}.", number);
        }

        public static void Main()
        {
            int num = 5;

            Console.WriteLine("num = {0}\n", num);

            Console.WriteLine("Passing num by value to method ModifyNumberVal() ...");
            ModifyNumberVal(num);
            Console.WriteLine("Value of num after exiting the method is {0}.\n", num);

            Console.WriteLine("Passing num by ref to method ModifyNumberRef() ...");
            ModifyNumberRef(refnum);;
            Console.WriteLine("Value of num after exiting the method is {0}.\n", num);
        }
    }
}

Example 1 – Passing Arguments by Reference

num = 5

Passing num by value to method ModifyNumberVal() ...
Value of number inside method is 15.
Value of num after exiting the method is 5.

Passing num by ref to method ModifyNumberRef() ...
Value of number inside method is 15.
Value of num after exiting the method is 15.

The program in Example 1 defined two methods that have the same purpose, to add 10 to the values passed to them. The first one  (lines 7-11) requires a parameter that accepts an argument’s value. When we call it and supply an argument  (line 26), the argument’s value is copied to the parameter of the  method. Therefore, the value of the original variable outside the method has no link or relation to the parameter of the method. We then added 10 to the parameter variable and print the result. To prove that the  num variable was not affected by the modification and it is a different copy, we print again its value and we saw that it did not change.

The second method (lines 13-17) requires the caller to pass the value by reference. Instead of copying the value of the argument being passed, the caller gives the address of that variable instead.  The parameter now “refers” to the original variable that was passed to the  method during the method call. When we modified the parameter variable containing the address of the original variable  (line 15), we are actually modifying the value of that original variable outside the method. We displayed the value of the original variable after exiting the method and it is apparent that the value was indeed modified.