1 class Program2 {3 //ref and out like C + + pointers, of course, can not be used directly when the pointer, is able to reference some parameters, but not directly as an address to control parameters (approximate meaning)4 Static voidMain (string[] args)5 {6 intA, B;7Outtest ( outA outb);8Console.WriteLine ("A={0},b={1}", A, b);9 //output A = 1;b = 2;Ten One intc = One, d = A; AOuttest ( outC outd); -Console.WriteLine ("A={0},b={1}", A, b); - //output c = 1;d = 2; the - intm = One, n = A; - //reftest (ref m, ref N); -Reftest (refMrefn); +Console.WriteLine ("M={0},n={1}", M, n); - //output m = 22;n = one; + A Swap (M, n); atConsole.WriteLine ("M={0},n={1}", M, n); - //output m = 22;n = one; - } - - Static voidOuttest ( out intX out inty) - { in //method can be used outside the body without assigning a value to the outgoing parameter - //The method body, must assign the value to the outgoing parameter, otherwise error, for example: x=y; to //functions that apply to multiple return values +x =1; -y =2; the } * $ Static voidReftest (ref intXref inty)Panax Notoginseng { - the //method Body, you must assign a value to the parameter, otherwise the error + //within the method body, ref can assign or not assign a value to a parameter A //suitable for referencing the original value and modifying the original value the inttemp =x; +x =y; -y =temp; $ } $ - Static voidSwap (intAintb) - { the inttemp =A; -A =b;Wuyib =temp; the } -}
Simple differences between C # ref and out