To pass by reference, C# provides the ref parameter modifier.
A parameter can be passed by reference or by value, regardless of whether the parameter is a reference type or a value type.
In the following example, p and x refer to the same memory locations:
using System; class MainClass/*from w w w .java 2s. c om*/ { static void Test (ref int p) { p = p + 1; // Increment p by 1 Console.WriteLine (p); // Write p to screen } static void Main() { int x = 8; Test (ref x); // x Console.WriteLine (x); // x is now 9 } }
assigning p a new value changes the contents of x.
ref modifier is required both when writing and when calling the method.