using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace InOutRef { static class InOutRef { public static void In(int i) { Console.WriteLine(i); i=100; Console.WriteLine(i); } public static void Ref(ref int i) { Console.WriteLine(i); i=200; Console.WriteLine(i); } public static void Out(out int i) {
I can not be more literal in my answer. When used , the code will not remember reference groups, such as the classic Java question exchange. However, when using ref, it will be similar to VB.NET, as it will remember the changes in and out . If you use the out parameter, it means that it must be declared before returning (this is done by the compiler).
Output:
1 // 1 from main
100 // 100 from in
1 // 1 is NOT remembered from In
200 // 200 from ref
// should be 200 here but out enforces out param (not printed because commented out)
300 // 300 is out only
Press any key to continue. . .
user295190
source share