Now that the file is already created, we will be opening it in the text editor. Let’s start with the first example illustration to use the “ref” keyword in the code. So, we have been using the c# standard structure for the code, which includes the use of the “System” library, some namespace, class, and its functions. We have named the namespace “Test” and named the class ‘Test’ as well. Within this class, we have been utilizing the two simple methods of C#. One of them is the built-in, main() function and the other one is the newly made user-defined “Modify” function.
As we all know, the program execution always initializes itself from the starting of the main() method. When we execute the code, the main() function is executed first. So, right after the execution started, the initialization of a variable “v” was made and assigned a value of 20. There comes the “Console” class of C# that provides us with the function “WriteLine” to display the data as an output on the screen. We have used this function to display the value of variable “v” at our shell in a second. After displaying the value of variable “v”, we have passed this variable as a reference to the function “Modify” with the keyword “ref”.
Now, whatever changes will be made in the Modify function will also reflect changes to the “v” variable. When we take a look at the “Modify” function, we came to know that it has also been using the “ref” keyword with the newly made variable “n” in the argument. This variable “n” has been referencing the memory address of variable “v” from the main() function. The value of “n” will be divided by 3 and saved to the variable “n,” i.e. eventually, the value of “v” will be used here. In the Modify function, we have utilized the “WriteLine()” function of the console class to display the value of “n” while executing the “modify” function. As control has been given to main(), the last WriteLine() function will display the updated value of variable “v”.
We need to have the “mcs” compiler and “mono” runtime for C# installed on the Ubuntu 20.04 system. We have been trying to use the “mcs” compiler command to compile this code. The code’s “exe” file got created, and we have tried to execute it with “mono” instruction. It turns out that the original value of variable “v” is 20, and after the execution of the Modify function, it becomes 6 within the “Modify” function and back in the main() function.
Let’s have a look at another C# simple example to use the “ref” keyword to perform value passing by reference. This time, we will be updating our code by adding 3 functions within a class “test” in the C# code. Within the Main() method of this program, we have initialized two integer variables at the start, i.e. x and y. Both the integer variables contain the same values, i.e. 7. We have tried to display the values of both variables x and y with the “WriteLine()” function of the Console class from the System library.
The first user-defined function, “Add1”, is called by passing its value “x”. Within this function, we have been adding 7 to the original value of x. After returning, we will display the value of “x” after the update again. The “Add2” function is called by passing variable “y” as referencing with the keyword “ref”. The same addition of 7 has been made in this function implementation, and the “ref” keyword is used in its function declaration line. On returning to the main(), the value of “y” has been displayed using the “WriteLine()” function after this update. Let’s save this code first and then run it.
Being compiled by the “mcs” query of C# and executed by the “mono” runtime, we have got the output as presented. The value of “y” is updated because “ref” was used while the “x” is unchanged.
Within our last illustration, we will be using the two classes Test and “New”. The “New” class started from the main() function and initialized an object “t” for the class “Test”. The “Test” class contains the declaration of two private variables “, x” and “y”, and it contains a parameterized constructor “, Test”, initializing variables “x” and “y”. When the object “t” has been created, the constructor of the “Test” class automatically gets executed. As this constructor is parametrized, we have passed it to values while creating an object. These values are saved to the “x” and “y” variables.
Now, we have been passing an object “t” as a reference to the function “Modify” of the “Test” class with the keyword “ref”. The Modify() function uses the “ref” keyword to get the “Test” class object as “obj” and uses this object to increment the value of variable “x” by 7 and variable “y” by 5. After returning to the main() function of “New” class, the WriteLine() function of System.Console class is used to display that we have updated the object and to display the updated values. In the last “WriteLine()” function statement, we have been using the “t” object to call the “getx” and “gety” functions from the “test” class to display the updated values. While looking at the “getx()” and “gety()” functions from the “Test” class, we have come to know that both contain the return statement to return their specific variable value, i.e. “x” and “y”. Let’s see the output of this script after saving it.
After the code execution, we have learned that the values of “x” and “y” variables have been successfully incremented after passing the objects as a reference using the “ref” keyword.
This article is utilized to explain the purpose of using the “ref” keyword in C# programming. We have discussed the first two simple C# examples to perform the variable value passing to the other method by the concept of value passing by referencing through “ref”. The last example is all about passing the class object as a reference to some method of another class with the “ref” keyword. All the examples share the same concept with a few twists and turns and different calculations.