C# Pass By Reference (using ref keyword) with Examples
When we pass a value type variable using ref
keyword the reference of the variable is passed and both variable names points to same memory location. So when the variable is updated it is available in both method.
It can be depicted pictorially as
Simple Program Using ref Keyword
using System; using System.Diagnostics; using System.Windows.Forms; namespace Tutlane { class Programm { [STAThread] static public void Main(string[] args) { int a = 10; //We have initialized before we pass it as an argument to the method Console.WriteLine("Variable Value Before Calling the Method: {0}", a); ProcessNumber(ref a); //Calling ProcessNumber() function with ref of variable a. Console.WriteLine("Variable Value After Calling the Method: {0}", a); } public static void ProcessNumber(ref int a) { a = 100; Console.WriteLine("Variable value Insdie the Method: {0} ",a); } } //End of Class Programm } //End of NameSpace Programm