A reference parameter argument passed by reference. Argument passed to a variable reference parameter must have a definite value, and the method during execution, a reference parameter indicates the storage location of the same argument. A reference parameter ref
modifier declared.
Output parameter argument passed by reference. Similar reference parameters and output parameters, except that the argument does not require the caller to provide explicit assignment. Output parameter out
modifier declared. The following example shows how to use each keywordref
out
using System; class RefExample { static void Swap(ref int x, ref int y) { int temp; temp = x; x = y; y = temp; } public static void SwapExample() { int i = 1, j = 2; Swap(ref i,ref j); Console.WriteLine($"{i}{j}"); } /*static void Main(string[] args) { SwapExample(); } */ } class OutExample { static void Divide(int x, int y,out int result,out int remainder) { result = x / y; remainder = x % y; } public static void OutUsage() { Divide(10, 3, out int res, out int rem); Console.WriteLine("{0} {1}", res, rem); } static void Main() { OutUsage(); RefExample.SwapExample(); } }