Ref is the address of the pass parameter, out is the return value, and the two have some similarities, but there are different points.
You must assign a value to a variable before using ref.
The Out function empties the variable, even if the variable has already been assigned a value, all out-referenced variables are assigned values when exiting the function, and ref references can be modified or unmodified.
The difference can be see the following code:
Static voidOuttest ( out intX out inty) {//before you leave this function, you must assign values to x and Y, otherwise you will get an error. //y = x; //The above line is an error because when out is used, X and y are emptied and need to be re-assigned, even if the function is assigned a value before calling it .x =1; Y=2; } Static voidReftest (ref intXref inty) {x=1; Y=x; } Public Static voidMain () {//Out Test intA, B; //variables may not be assigned before out of useOuttest ( outA outb); Console.WriteLine ("A={0};b={1}", A, b); intc = One, d = A; Outtest ( outC outd); Console.WriteLine ("c={0};d ={1}", C, D); //ref Test intm, N; //reftest (ref m, ref N); //The row above is faulted, and the variable must be assigned before ref is used into = One, p = A; Reftest (refOrefp); Console.WriteLine ("o={0};p ={1}", O, p); }
The role of the Ref,out keyword