C# offers the ref keyword as reference type for passing the argument. It passes references of argument to the function instead of copying the original value. The change in passed values is permanent and changes the original value of the variable.
Example:
using System;
namespace CallByReference
{
class Program
{
// User defined function
public void Display(ref int val)
{
val *= val; // Manipulating value
Console.WriteLine("Value inside the Display function " + val);
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
int val = 20;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before calling the function " + val);
program.Display(ref val); // Calling Function by passing value
Console.WriteLine("Value after calling the function " + val);
}
}
}
Output:
Value before calling the function 20
Value inside the Display function 400
Value after calling the function 400