When you use a method to get the return value in C #, you usually get only one return value. Therefore, when a method needs to return multiple values, it needs to use ref and out, then what is the difference between these two methods?
Msdn:
The REF keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the REF keyword.
The Out keyword causes parameters to be passed by reference. This is similar to the REF keyword, except that the ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the Out keyword.
Case:
Defines a method that evaluates to the maximum, minimum, and average values in an array of integers. If there is only one return value for a method, it is only possible to define a method for each one, but with ref and out it is much easier to implement.
Ref
1 static int getintresult (int[] arry, ref float AVG, ref int max, ref int min) 2 {3 int sum = 0; 4 max = ARRY[0]; 5 min = arry[0]; 6 for (int i = 0; i < Arry. Length; i++) 7 {8 sum + = Arry[i]; 9 if (Max < arry[i]) one {ten max = arry[i];13 }14 if ( Min > Arry[i]) { min = arry[i];17 }18 }19 avg = Sum/arry. Length;20 return sum;21 }
Then try calling the method in the console:
1 static void Main (string[] args) 2 {3 int[] arr = {1,2,3,4,5,6,7,8,9};4 float avg;5 int max;6 int min;7 int sum = Getintresult (arr, ref AVG, ref max, ref min); 8 }
At this point the compiler will prompt for the red line, error: Using Unassigned Avg,max,min
1 static void Main (string[] args) 2 {3 int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 4 float avg = 0; 5< C14/>int max = 0; 6 int min = 0; 7 int sum = Getintresult (arr, ref AVG, ref max, ref min); 8 Console.WriteLine ("and: {0}\t mean: {1 }\t Maximum value: {2}\t min: {3} ", Sum, AVG, Max, min); 9 console.read ();
Operation Result:
Summarize:
Ref this keyword tells the C # compiler to pass a parameter value that points to the same memory as the variable in the calling code. Thus, if the called method modifies the values and returns, the variables of the calling code are modified.
The REF keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes to the parameters in the method are reflected in the variable (the initial value of Avg,max,min is 0, and the value changes after the call method). To use the ref parameter, both the method definition and the calling method must explicitly use the REF keyword.
Out
After switching out, the above method is no longer applicable, error: Control must be assigned to the Out parameter "Min" and "Max" before leaving the current method. You will find here that Max and Min are not initialized outside of the loop. That's why it goes wrong.
Modified code:
View Code
The result is the same as above.
Summarize:
The Out keyword causes parameters to be passed by reference. This is similar to the REF keyword, except that the ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the Out keyword.
Conclusion:
The only difference between the keyword "ref" and "out" is that the keyword "out" does not require the calling code to initialize the parameter values to be passed. So when does the keyword ' ref ' work? You should use the keyword "ref" When you need to make sure that the calling method has initialized the parameter value. In the example above, the ability to use "out" is because the method being called does not depend on the value of the passed variable. The taste is slowly realized ...
[C # base]ref and out differences