In C # If you need to convert a value type to a reference type and pass other methods and make its original value change, use ref and out to convert to a reference type pass.
1. Ref: A variable needs to be defined and initialized before using ref (must be initialized)
Class program { static void Main (string[] args) { int i = 10;//define variable and initialize; Console.WriteLine ("i =" + i); Print Demo before delivery (ref i); By ref I change the reference pass Console.WriteLine ("i =" + i); Console.readkey (); } The Receive method parameter also uses the REF keyword public static void Demo (ref int i) { i = i + +; } }
Printing results:
2. Out: You need to define variables before using out, do not initialize; Initialization is left to the called method.
Class program { static void Main (string[] args) { int i;//define variable [without initialization], if initialized here, the receiving method will be replaced; Demo (out i); By passing out I change the reference pass Console.WriteLine ("i =" + i); Console.readkey (); } The Receive method parameter also uses the OUT keyword public static void Demo (out int i) { i = 100;//using the Out method initial work is done inside the methods i = i + 200;< c14/>//I + max = +} }
The printing results are as follows:
[Use of C # Foundation]ref and out