C # ref, out, params, and value type parameter modifier,
1. Value Type:
1 static void Main (string [] args) 2 {3 int a = 5; 4 int B = 3; 5 NumVal (a, B); 6 Console. writeLine ("a = {0}, B = {1}", a, B); // The output result is: a = 5, B = 3 7 8 Console. readKey (); 9} 10 11 12 static void NumVal (int a, int B) 13 {14 a = a + B; 15 B = a-B; 16}
Code
When the value type is treated as a parameter, a copy of the value is passed. Therefore, modifying the parameter value in the following method does not affect the value of the parameter specified in the function call.
Of course, you can also define the return value for the function to get some desired results:
1 static void Main (string [] args) 2 {3 int a = 5; 4 int B = 3; 5 int num1 = NumVal (a, B ); 6 int num2 = NumValC (a, B); 7 Console. writeLine ("a = {0}, B = {1}", num1, num2); // The output result is: a = 8, B = 2 8 9 Console. readKey (); 10} 11 12 static int NumVal (int a, int B) 13 {14 a = a + B; 15 return; 16} 17 18 static int NumValC (int a, int B) 19 {20 B = a-B; 21 return B; 22}
Code
But it is not good to change the values of multiple variables used as parameters. (Because the function has only one return value ).
2. Reference Type (ref ):
1 static void Main (string [] args) 2 {3 int a = 5; 4 int B = 3; 5 NumVal (ref a, ref B); 6 Console. writeLine ("a = {0}, B = {1}", a, B); // The output result is: a = 8, B = 2 7 8 Console. readKey (); 9} 10 11 static void NumVal (ref int a, ref int B) 12 {13 int c = a; // c = 514 a = a + B; // a = 5 + 315 B = c-B; // B = 5-316}
Code
In this case, you can pass the parameter through the "Reference" parameter modifier (that is, the keyword ref, in this way, function processing is the same as the variable used in function calls (because the reference type parameter passes the reference address ), therefore, any modification to this variable in the method will affect the value of the variable used as the parameter.
However, variables modified by the ref modifier must be initialized before being passed as function parameters.
3. Output type (out ):
1 static void Main (string [] args) 2 {3 int a; 4 int B; 5 NumVal (out a, out B); 6 Console. writeLine ("a = {0}, B = {1}", a, B); // The output result is: a = 15, B = 4 7 8 Console. readKey (); 9} 10 11 static void NumVal (out int a, out int B) 12 {13 a = 5; B = 3; // initialize the output parameter 14 15 int c = a; 16 a = a * B; 17 B = (c + B)/2; 18}
Code
The usage of the out keyword is similar to that of the ref keyword, so that the parameter is passed by reference.
Difference: the ref parameter must be initialized before being used for parameter passing. The out parameter does not need to be initialized before transmission (even if it has been initialized before transmission, it needs to be re-initialized in the function to ignore the value initialized before transmission ), however, the value must be assigned before the return of the method ends.
4. Parameter array (params ):
1 static void Main (string [] args) 2 {3 int I = 0; 4 int [] arr = new int [] {1, 3, 5, 7, 9 }; // You can first define an array object 5 6 SumVals (ref I, arr); // then pass the array object as a parameter 7 8 Console. writeLine ("Total {0} elements! ", I); // The output result is: there are 5 elements in total! 9 10 SumVals (ref I, 2, 4, 6, 8, 0); // You can also directly call the 11 12 Console. readKey (); 13} 14 15 static void SumVals (ref int I, params int [] vals) 16 {17 for (I = 0; I <vals. length; I ++) 18 {19 Console. writeLine ("the {0} element is: {1}", I + 1, vals [I]); 20} 21}
Code
The function in C # can specify a specific parameter (that is, an array of parameters modified by params, which can only have one parameter ), this parameter must be the last parameter in the function definition (that is, no other parameter is allowed after the params keyword ).
The parameter array modified by params does not specify the number of parameters. The only restriction is that all parameter types must be the same as parameter arrays.