A value parameter: When using a parameter, a value is passed to a variable used by the function. Any modification to this variable in a function does not affect the parameters specified in the function call. (Because the function has only one return value, it cannot be used as a parameter value for multiple variables.)
Reference parameter: The variable that the function handles is the same as the variable used in the function call, not just a variable with the same value. Therefore, any change to this variable will affect the value of the variable used as a parameter. Parameters are specified with the REF keyword. The variable used as a ref parameter has two restrictions, because the function may change the value of the reference parameter, and all must use the "extraordinary" variable in the function call. Second, you must use the initialized variable.
Output parameter: Out keyword, specifies that the given parameter is an output parameter. The Out keyword is used in the same way as the ref keyword, and in fact, he executes exactly the same way as a reference parameter because the value of the parameter is returned to the variable used in the function call after the function has finished executing.
Iv. some important differences between reference parameters and output parameters:
- It is illegal to use an unassigned variable as a ref parameter, but an unassigned variable can be used as an out parameter.
- In addition, when a function uses an out parameter, it must be treated as if it has not been assigned a value. That is, the calling code can use an assigned variable as an out parameter, but the value stored in the variable is lost when the function executes.
The following three methods are used to describe three parameter passes:
Static voidAmethod (intI//Value Passing{i= i +1; } Static voidBmethod (ref intI//Reference Delivery{i= i +1; } Static voidCmethod ( out intI out stringJ//Output Pass{i=6;//the output parameter function must be initialized within the function to assign the valuej ="return"; } Static voidMain (string[] args) { inti =1;stringj ="C #";//Output parameter AssignmentAmethod (i);//Call value transfer functionConsole.WriteLine ("(1), I=1; After the Amethod method (plus 1), the value is passed i="+i); Console.WriteLine (); Bmethod (refi);//invoking a reference transfer functionConsole.WriteLine ("(2), I=1; after Bmethod method (plus 1), ref is passed i="+i); Console.WriteLine (); Cmethod ( outI outj);//invoking the output transfer functionConsole.Write ("(3), I=1; after Cmethod method (plus 1), after passing"); Console.WriteLine ("i="+ i +"j="+j); Console.WriteLine (); Console.readkey (); }
The results are shown below:
C # value parameters, reference parameters, and output parameters