c sharp

C# Pass by Reference

While working in some object-oriented programming, you may have tried the concept of value passing by reference in the codes, i.e. C, C++. Pass by reference means to indicate the memory location of a certain variable without creating its copies. The references are most probably used to perform certain changes and make them reflect at the original memory location without any issue. We tend to use the “&” character in object-oriented languages to perform pass-by references.

Within C#, we perform the passing values by reference using the “ref” keyword. Let’s see how it works. Starting with implementing this guide, we must have a C# file to create some programming codes in it. So far, so good; we have been using Ubuntu’s “touch” query to generate a new C# file in the current working directory of our Linux system.

Example 01

We will be starting our first example of this code with the C# program structure definition. So, we opened the C#, created a file in the built-in text editor of Ubuntu 20.04, and added the “System” library at its first line. This library is the main input and output source within the C# codes. Without this library, our code has no use to us because it will not work the way we want. So, after adding the library, we have created a random namespace “Reference” in this code. This namespace is user-defined.

We have also created a new user-defined class, “Reference”, in this program to initialize our code with some implementation. This user-defined “Reference” class contains a total of 2 functions in it. The first function is the built-in main() function with return type “static void”, which is also said to be the driver function of our C# code, i.e. all execution starts from it. The other function is a user-defined random function named “Update”, having the return type “public static void”.

The Console.WriteLine() function is here to display the original value of a variable “n” utilizing its parameters as shown below in the image. After this, we called the “Update” function from the main passing variable “n” as a parametric value. Now, the control goes to the “Update” function, which has been taking the value of variable “n” as a reference using the new variable “val” of integer type. This variable “val” will multiply its passed value “5” from the main() function by “3” and overwrite to the variable “val”.

There is no return statement here. The execution of the Update function has been completed, and control is back to the “main” function. The main() function executes its next Console.WriteLine() function to display the updated value of variable “n” using “n” in its parameters. The main() function and our C# code are complete and ready to be used. Let’s save it by Ctrl+S and exit it with Ctrl+X first.

After saving the C# file, the first step we have to make is to compile this code with the C#’s “mcs” compiler already configured in our Ubuntu 20.04 system. So, we did so, and the “exe” file for this compiled code has been automatically created in the same location. We have used the “mono” runtime execution instruction for C# codes to execute this “exe” file and got the shown-below result. It turns out that the original value and the updated value are the same, i.e. no changes have been made so far.

Let’s check out the problem now. When we try to update the value of variable “n” by passing it as a reference, it doesn’t locate the memory address of the original variable “n”. Instead, it created another replica object with the same name and value and passed it to the update function. Thus, changes made to the new object don’t affect the original variable “n” value.

To avoid this issue, the C# framework came up with the new keyword “ref” to pass values as a reference to any function parameter. Using this “ref” keyword, the original variable memory location has been used instead of the new memory address, i.e. pointing towards the memory location of the original variable. Thus, we have updated our code and used the ‘ref’ keyword while passing the variable “n” as a reference to the update function. This keyword has been used in the main() function while the function call to the “Update” function and in the “Update()” function parameters as displayed.

After compiling the updated C# file and running its “exe” just after that, we have been presented with the shown output. This time the value “5” of variable “n” has been updated to “15” due to the use of the “ref” keyword to pass a value as a reference.

Example 02

Let’s look at another example to perform swapping by passing values in parameters. The class “Test” has been using a user-defined “swap” function and the main() driver function. The execution started from the main() method where we have created an object “t” of class Test and initialized two integer variables, v1 and v2, with integer values—the Console.WriteLine() function statements display the values of variables v1 and v2 before the swap.

After that, the swap function is called by passing variables v1 and v2 in its parameters as reference. The “ref” keyword has been utilized here for both variables. Within the “Update” function implementation, we have also used the “ref” keyword for both variables n1 and n2 to pass values by reference. To swap values of one variable to another, we must have a third value to perform this. The new variable “v” has been initialized within this function, taking the value of variable n1 as the first part. The value of n2 has been overwritten into variable n1, and the value of variable v has been saved to variable n2. The swapping of value n1 with n2 has been done here. The main() function Console.WriteLine() function is here to now display the newly swapped values of n1 and n2.

After the compilation and execution of this example code, we have got values of v1 and v2 as 14 and 7 before the swap. After the swap, it turns out to be the opposite, i.e. v1 = 7 and v2=14.

If we remove the “ref” keyword from the function call and the function definition of the “Update” function as we did below, the swapping will not be performed.

Here you can see the output shows that the values of v1 and v2 are the same even after using the swap function.

Conclusion

This guide is all about using the “ref” keyword in C# to perform passing values by a reference. This referencing has been done by using the exact memory location of certain variables. Within this guide, we have discussed using the “ref” keyword to update the original value of a variable by multiplying it with some number and swapping the values of two variables in two different examples. This has been performed very smoothly with “ref”.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.