Link Search Menu Expand Document

Pass by Value vs Pass by Reference in C Sharp

In C#, there are two ways to pass arguments to a function call: by value and by reference. In this article, you will see how to pass function arguments by value and by reference. You will also see the difference between the two methods. So, let’s begin without any ado.

Passing Arguments by Value

When you pass a function argument by value, a copy of the variable passed as an argument is created inside the function. Operations performed on the copy of the argument variable inside the function are not reflected outside the function.

This is best explained with the help of an example. Look at the following script.

In the script below, you define a variable “a” inside the Main method. This variable is passed as an argument to the method PassByValue() (you can name the method anything). Inside the PassByValue() method, a copy of the argument “a” is generated which is defined as “num”. The “num” variable is multiplied by 2 and its value is printed on the Console. Since the variable “num” stores the copy of the argument variable “a”, any change to the variable “num” is not reflected in the variable “a”.

class Program
{
static void Main(string[] args)
{

int a = 10;
Console.WriteLine("Variable value before passing to function: " + a);
PassByValue(a);
Console.WriteLine("Variable value after passing to function: " + a);
Console.ReadKey();
}

public static void PassByValue(int num)
{
num = num * 2;
Console.WriteLine("Variable value inside the function: " + num);
}

}

You can see from the below output that the value of the variable changes only inside the function and it remains the same before and after the function call.

C Sharp Pass by Value

Passing Arguments by Reference

Let’s now see what happens when you pass an argument value by reference. When you pass an argument value by reference to a function call, the reference or memory location of the argument is passed to the function call. Any changes made to an argument value inside the function are reflected in the original argument value passed in the function call.

To pass an argument to a function by reference, you need to make two changes. Inside the function call, you have to prefix a keyword “ref” before the parameter name which accepts an argument value by reference. Secondly, when you call a function and pass it an argument value by reference, you need to prefix the keyword “ref” before the argument value. Here is an example.

In the script below, the variable “a” is passed as an argument to the function “PassbyRef()”. The reference of the variable “a” is actually passed to the “num” variable. Therefore, when the value of the “num” variable is updated, the change is reflected in the variable “num”.

class Program
{
static void Main(string[] args)
{

int a = 10;
Console.WriteLine("Variable value before passing to function: " + a);
PassByRef(ref a);
Console.WriteLine("Variable value after passing to function: " + a);
Console.ReadKey();
}

public static void PassByRef(ref int num)
{
num = num * 2;
Console.WriteLine("Variable value inside the function: " + num);
}
}

The output shows that the value of the variable “a” once updated by the “PassByRef()” method is reflected outside the function call as well.

C Sharp Programming Pass by Value

Custom Objects Variable are Passed By Reference

It is important to mention that member variables of custom objects are always passed by reference without even using the “ref” keyword.

For instance, in the following script, the variable “age” of the “Student” class object is passed by reference to the “PassByRef()” function without prefixing the “ref” keyword before the function call.

class Program
{
static void Main(string[] args)
{

Student s = new Student();
s.age = 5;

Console.WriteLine("Variable value before passing to function: " + s.age);
PassByRef(s);
Console.WriteLine("Variable value after passing to function: " + s.age);
Console.ReadKey();
}

public static void PassByRef(Student s)
{
s.age = s.age * 2;
Console.WriteLine("Variable value inside the function: " + s.age);
}

}

public class Student
{
public int age;
}

Here is the output of the above script. You can see that the value of the Student class’s object variable “age” once updated inside the “PassByRef()” method remains unchanged.

C Sharp Pass by Reference

Other useful articles:


Back to top

© , C Sharp Online — All Rights Reserved - Terms of Use - Privacy Policy