Formula: Ref has a in, out only out!
In C #, parameters can be passed either by value or by reference. Passing parameters by reference allows a function member to change the value of a parameter and keep the change. To pass parameters by reference, you can use the ref or out keyword for the corresponding parameter. It works much like a pointer in C.
Usage of ref:
1 namespaceConsoleApplicationTest201601252 {3 class Program4 {5 Static voidMain (string[] args)6 {7 stringA="Initialize! ";8Refmethod (refa);9Console.WriteLine ("usage test for ref keyword! \ t");TenConsole.WriteLine ("results: a="+a); One Console.readkey (); A } - - Private Static voidRefmethod (ref stringa) the { -A ="executed the ref! "; - } - + -}
View Code
Operation Result:
Usage test for ref keyword!
Results: A= performed the ref!
Use of Out:
1 namespaceConsoleApplicationTest201601252 {3 class Program4 {5 Static voidMain (string[] args)6 {7 stringb//No initialization required8Outmethod ( outb);9Console.WriteLine ("usage test for out keyword! \ t");TenConsole.WriteLine ("results: b="+b); One Console.readkey (); A - } - the Private Static voidOutmethod ( out stringb) - { -b ="executed the out!"; - } + } -}
View Code
Operation Result:
Usage test for Out keyword
Results: B= performed the out!
Usage and distinction of ref and out and params usage