Cainiao notes-ref and out parameters (parameter transfer), refout
1. Introduction Problems
Example: Write a method to calculate the sum and maximum and minimum values of each element in the int array.
* The purpose is to return three value-type variables. The problem arises, but the return method can only return one value. In this case, you can use ref and out to modify the parameter.
Sample Code (not modified ):
1 static void Main (string [] args) 2 {3 4 5 int [] arr = {56,31, 2,89, 76}; 6 int sum = 0, max = 0, min = 0; 7 GetValue (arr, sum, max, min); 8 9 Console. writeLine ("sum:" + sum + ", maximum value:" + max + ", minimum value:" + min); 10} 11 12 public static void GetValue (int [] arr, int sum, int max, int min) 13 {14 sum = 0; 15 max = arr [0]; 16 min = arr [0]; 17 18 for (int I = 0; I <arr. length; I ++) 19 {20 sum + = arr [I]; 21 if (max <arr [I]) 22 {23 max = arr [I]; 24} 25 if (min> arr [I]) 26 {27 min = arr [I]; 28} 29 30} 31 32}View Code
Sample Code (after modification ):
1 static void Main (string [] args) 2 {3 4 5 int [] arr = {56,31, 2,89, 76}; 6 int sum = 0, max = 0, min = 0; 7 GetValue (arr, ref sum, ref max, ref min); 8 9 Console. writeLine ("sum:" + sum + ", maximum value:" + max + ", minimum value:" + min); 10} 11 12 public static void GetValue (int [] arr, ref int sum, ref int max, ref int min) 13 {14 sum = 0; 15 max = arr [0]; 16 min = arr [0]; 17 18 for (int I = 0; I <arr. length; I ++) 19 {20 sum + = arr [I]; 21 if (max <arr [I]) 22 {23 max = arr [I]; 24} 25 if (min> arr [I]) 26 {27 min = arr [I]; 28} 29 30} 31 32}View Code
The result shows that the value type parameter passed by ref and out is not used, and the value content in the stack memory is passed.
* After modification with ref and out, the value type transfer will pass the stack memory address, which leads to the form parameter and the real parameter (the real parameter is assigned to the form parameter), and only one address is assigned,
* After the parameters are changed through the method, the value page of the parameters changes.
2. ref = references --- reference
* Note: 1. Parameters for passing parameters must be declared and assigned a value before the method is called (values must be assigned first)
2. Add ref for passing parameters, and add ref for the parameters declared in the method ".
3. The parameter passed by ref does not actually create a new variable. Instead, it shares the real parameter and the form parameter with the memory of an address.
3. out = Output Parameters
* Note: 1. the parameter passing can only declare that the parameter is not assigned a value, but must be assigned a value in the called method (the parameter must be assigned in the method body)
2. The parameter passing parameter must be added out, and the parameter declared by the method must also be added out ".
3. The parameter passed by the out operation does not actually create a new variable. Instead, it shares the real parameter and the form parameter with the memory of an address.
4. Differences between ref and out:
The parameter passed by ref must be assigned a value)
An out statement can only declare that no value is assigned, but it must be assigned a value (internal value assignment) in the method body)
5. Basic concepts:
1. parameter transfer. By default, stack space content is transmitted.
2. If the ref or out modifier is added, the address of the stack space will be passed, instead of the stack space content.
Transfer Process: The form parameter is a standard of the real parameter, and the form parameter is also an independent variable. The real parameter will be assigned to the form parameter.