The ref keyword in C# is used for passing or returning references of values to or from Methods. Basically, it means that any change made to a value that is passed by reference will reflect this change since you are modifying the value at the address and not just the value. It can be implemented in the following cases:
- To pass an argument to a method by its reference.
- To define a method signature to return a reference of the variable.
- To declare a struct as a ref struct
- As local reference
Example 1: Here, we define two methods addValue and subtractValue. The method addValue is a method that only modifies the value of its parameter. Therefore when the value of ‘a’ is displayed after passing it as a parameter to addValue there is no difference in its value. Whereas the method subtractValue uses the reference of the parameter, the keyword ref indicating the same. Hence when the value of ‘b’ is displayed after passing it as a parameter to subtractValue, you can see the changes are reflected in its value. The ref keyword must be used in the method definition as well as when the method is called.
// C# program to illustrate the // use of ref keyword using System;
namespace ref_keyword {
class GFG {
// Main Method
static void Main( string [] args)
{
// Initialize a and b
int a = 10, b = 12;
// Display initial values
Console.WriteLine( "Initial value of a is {0}" , a);
Console.WriteLine( "Initial value of b is {0}" , b);
Console.WriteLine();
// Call addValue method by value
addValue(a);
// Display modified value of a
Console.WriteLine( "Value of a after addition" +
" operation is {0}" , a);
// Call subtractValue method by ref
subtractValue( ref b);
// Display modified value of b
Console.WriteLine( "Value of b after " +
"subtraction operation is {0}" , b);
}
// Define addValue
// Parameters passed by value
public static void addValue( int a)
{
a += 10;
}
// Define subtractValue
// Parameters passed by ref
public static void subtractValue( ref int b)
{
b -= 5;
}
} } |
Initial value of a is 10 Initial value of b is 12 Value of a after addition operation is 10 Value of b after subtraction operation is 7
Example 2: You may use the keyword ref with an instance of a class as well. In the program given below, we have a class Complex to represent Complex numbers. The class also consists of an update method that uses the object’s reference and reflects the updates made in the value of the real and imaginary part of the object.
// C# program to show the use of ref // with an instance of a class using System;
namespace class_ref {
class Complex {
private int real, img;
// Parameterize Constructor
public Complex( int r, int i)
{
real = r;
img = i;
}
// Method to get value of real
public int getRealValue()
{
return real;
}
// Method to get value of img
public int getImgValue()
{
return img;
}
// Method to update value of complex
// object Using reference of the object
public static void Update( ref Complex obj)
{
obj.real += 5;
obj.img += 5;
}
} class GFG {
// Main Method
static void Main( string [] args)
{
// Declare Complex object
Complex C = new Complex(2, 4);
Console.WriteLine( "Complex number C = " + C.getRealValue() +
" + i " + C.getImgValue());
// Call update method
// Pass ref of C
Complex.Update( ref C);
Console.WriteLine( "After updating C" );
Console.WriteLine( "Complex number C = " + C.getRealValue() +
" + i " + C.getImgValue());
}
} } |
Complex number C = 2 + i 4 After updating C Complex number C = 7 + i 9