I. out parameter instance
[example] find the maximum value, minimum value, total value and average value in an array
class Program { static void Main(string[] args) { //Write a method to find the maximum value, minimum value, total value and average value in an array int[] nums = { 1, 2, 3, 4, 5, 6 ,7}; int max; int sum; int min; int avg; bool b; string s; double d; GetValue(nums, out max, out min, out sum, out avg,out b,out s,out d); Console.WriteLine("Maximum in array:{0},Minimum:{1},The sum:{2},average value:{3}",max ,min,sum,avg); Console.WriteLine("output out The method of the parameter has different types of values:{0},{1},{2}",b,s,d); Console.ReadKey(); } /// <summary> ///Find the maximum, minimum, sum and average of an array of integers /// </summary> ///< param name = "nums" > array of required values < / param > ///< param name = "Max" > maximum value returned < / param > ///< param name = "Min" > minimum value returned < / param > ///< param name = "sum" > total returned < / param > ///< param name = "AVG" > average value returned < / param > public static void GetValue(int[] nums, out int max, out int min, out int sum, out int avg, out bool b,out string s,out double d) { //The out parameter requires a value to be assigned inside the method max = nums[0]; min = nums[0]; sum = 0; for (int i = 0; i < nums.Length; i++) { if (nums[i]>max) { max = nums[i]; } if (nums[i]<min) { min = nums[i]; } sum += nums[i]; } avg = sum / nums.Length; b = true; s = "123"; d = 3.13; } }
Execute the code output, as shown in the figure:
Conclusion:
If a method returns multiple values of the same type, we can consider returning an array. However, when multiple values of different types are returned, it is not good to return arrays. At this time, we can consider using the out parameter.
The out parameter focuses on the fact that a method can return multiple values of different types.
2. Examples of ref parameters
[example] use method to exchange two variables of type int
class Program { static void Main(string[] args) { //Use method to exchange two variables of type int int n1 = 10; int n2 = 20; Test(ref n1, ref n2); Console.WriteLine("After two values are exchanged n1 by{0},n2 For:{1}",n1,n2); Console.ReadKey(); } public static void Test(ref int n1,ref int n2) { int temp = n1; n1 = n2; n2 = temp; } }
Execute the code output, as shown in the figure:
Conclusion:
ref parameter can bring a variable into a method for change. After the change, the changed value will be brought out of the method.
The ref parameter requires a value to be assigned outside the method, but not inside the method.
III. params variable parameters
[example] find the sum of any length array and integer type
class Program { static void Main(string[] args) { //Find the sum of any length array int[] nums = { 1, 2, 3, 4, 5, 6 }; int sum = GetSum(8,9); Console.WriteLine(sum); Console.ReadKey(); } public static int GetSum(params int[] nums) { int sum = 0; for (int i = 0; i < nums.Length; i++) { sum += nums[i]; } return sum; } }
Execute the code output, as shown in the figure:
As shown in the figure:
Conclusion:
Treat the elements in the real parameter list consistent with the variable parameter array type as the elements of the array
Params variable parameters must be the last element in the parameter list, and only one params keyword is allowed in the method declaration.
IV. difference between out and ref
Difference:
1. When using the ref parameter, the passed in parameter must be initialized. For out, it must be initialized in the method.
2. When using the ref parameter and out parameter, the ref or out keyword should be added to both the method parameter and the execution method.
3. The out parameter is suitable for places where multiple return values need to be returned, while the ref parameter is used when the method to be called needs to modify the reference of the caller.
As shown in the figure:
out parameter
ref parameter
The ref keyword needs to initialize the passed in parameters before it can be used.