Understanding C# Pass by Reference and Pass by Value

c# pass by referenceC# is an object oriented language architected by Microsoft for the development of a variety of applications that run via .NET framework.  Like every object oriented language, C# contains objects, methods, variables, properties and events. And similar to other object oriented languages, C# methods take parameters. There are two ways in which parameters can be passed to a method in C#: Pass by value and pass by reference. This article describes basic difference between the two. The article contains basic examples that demonstrate how a parameter is passed by reference and by value, along with their implications.

Interested in learning more about OOP in C#? Take this course at Udemy.com

C# Pass by Value

Passing parameters to a method by value is simple. That is why this is explained before explaining passing parameters by reference, which is more complex. C# pass by reference has been explained in the next section.

When a simple variable is passed as the parameter to any method, it is passed as a value. This means that the value contained by the variable that is passed as the parameter is copied to the variables of the method, and if inside the method these values are changed or modified, the change is not reflected in the actual passed variable.

Passing variable by value is useful in cases where the actual value of the variable should not be modified by the method and the change is only limited to the called method whereas the value of the variables in the calling method remain unchanged.

The following example demonstrates the concept of passing variable by value in C#.

class Program

{

static void Square(int a, int b)

{

a = a * a;

b = b * b;

Console.WriteLine(a +" "+b);

}

 

static void Main(string[] args)

{

 

int num1 = 5;

int num2 = 10;

 

Console.WriteLine(num1 +" "+num2);

Square(num1, num2);

Console.WriteLine(num1 + " " + num2);

Console.ReadLine();

}

}

In the above example, the class Program contains two static methods. One is the Main method that is the entry point of the application and the other method is the Square method that takes two parameters and displays the squares of these values.

Inside the main method, two integer type variables num1 and num2 have been declared and assigned values of 5 and 10 respectively. These values have been displayed on the console output.

Next, num1 and num2 have been passed as parameter to Square method. The square method copies the values of num1 and num2 variable into variables in the parameter body i.e ‘a’ and ‘b’ respectively. Now ‘a’ and ‘b’ variables only contain copies of the values of num1 and num2 variables passed to the method. Other than that variables  ‘a’ and ‘b’ have no connection with num1 and num2 respectively and if the value of variable ‘a’ and ‘b’ are changed it will have no impact on num1 and num2.

Inside the square method, the square of ‘a’ and ‘b’ values have been squared and result is stored in ‘a’ and ‘b’ variables respectively. These values have then been displayed on the console.

Now, if inside the main method, after calling the Square method, if num1 and num2 variables are displayed, they would contain their original values of 5 and 10.

The output on console would look like this:

5 10

25 10

5 10

New to C#? See this beginner’s course at Udemy.com

C# Pass by Reference

Passing a variable to a method by reference is a little bit trickier. The concept of C# pass by reference is that when a parameter is passed to a method by reference, instead of passing value contained by a variable, the reference of the variable is passed to method. The method operates on the references of the variables passed in the parameters rather than operating on their values. This results in the modification of variables in the calling function when they are modified in the called function.

Normally, all the objects are passed by reference as parameter to the method. On the other hand most of the primitive data types such as integer, double, Boolean etc. are passed by value.

The following example demonstrates the concept of C# pass by reference.

class Person

{

public int age;

}

 

class Program

{

static void Square(Person a, Person b)

{

a.age = a.age * a.age;

b.age = b.age * b.age;

Console.WriteLine(a.age+" "+b.age);

}

 

static void Main(string[] args)

{

 

Person p1 = new Person();

Person p2 = new Person();

 

p1.age = 5;

p2.age = 10;

 

Console.WriteLine(p1.age +" "+p2.age);

Square(p1, p2);

Console.WriteLine(p1.age + " " + p2.age);

Console.ReadLine();

}

}

 

In the above code, a class named Person has been declared and it only contains one public member of type integer named age.

Inside the Main method of the Program class, two objects of this Person class have been created and have been named p1 and p2. The member variable age of p1 is assigned a value of 5 and the member variable age of p2 is assigned value 10. These two values have then been displayed on the console.

Next, the two person objects p1 and p2 have been passed as parameters to Square method. This is where real magic begins. What happens here is that instead of passing the values of the members of the p1 and p2 objects, the reference of these objects is passed to the Square method. Those references would be copied to ‘a’ and ‘b’ person type objects in the parameters of the Square method.

Inside the Square method, the age variable of both the a and b objects would be accessed and squared. The resultant values would be again stored in the age variable of both objects and would be displayed on console.

Now, after passing p1 and p2 objects to Square method, if the value of age member variable of these two objects is displayed in the Main method, they would contain the updated value. This is due to the reason that Inside the Square method the member variable ‘age’ has been updated using the reference of the p1 and p2 object that was passed to it.

The output would look like this:

5 10

25 100

25 100

Conclusion

Passing by reference is an extremely important feature of C#.NET. It allows modifying multiple values inside a function, which otherwise would return only one value. Apart from that, a complex application might contain multiple classes and methods, it is never advisable to create and destroy objects again and again. The better approach is to create the object of a class and pass it as reference between multiple parts of the application.

For more essential C# tutorials, check out this course at Udemy.com