A, out parameters examples
[Example] selecting the maximum value in an array, minimum, sum, average
Program class { static void the Main (String [] args) { // Write a method of selecting the maximum value in an array, minimum, sum, average int [] nums = {1, 2, 3, 4, 5, 6 ,. 7}; int max; int SUM; int min; int AVG; BOOL B; String S; Double D; the GetValue (the nums, OUT max, OUT min, OUT SUM, OUT AVG, OUT B, OUT S, OUT D) ; Console.WriteLine ( "maximum value of the array: {0}, the minimum value: {1}, the sum of: {2}, the average value: {}. 3", max, min, sUM, AVG); Console.WriteLine ( "different types of method of outputting out parameter values: {0}, {. 1}, {2}", B, S, D); the Console.ReadKey (); } /// <Summary> /// selecting the maximum value of an integer array, minimum, sum, average /// </ Summary> /// <param name = "the nums"> requires array value </ param> /// <param name = "max"> maximum return </ param> /// <param name = "min"> minimum </ param> return /// <param name = "sum" > return sum </ param > /// <param name = "AVG"> average return </ param> public static void the GetValue (int [] the nums, int max OUT, OUT min int, int SUM OUT, int AVG OUT, B OUT BOOL, String S OUT, OUT Double D) { // OUT parameter requirements must be assigned values in the internal method max = the nums [0]; min = the nums [0]; SUM = 0; for (int I = 0; I <the 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; } }
Execution code output result, as shown:
to sum up:
If a method, a plurality of the same type of return value, we can consider returning an array. But a number of different types of return values when it returns an array not, then this time, we can consider the use of out parameters.
out parameters to focus on a way to return a plurality of different types of values.
Second, the parameters of Examples ref
[Examples] The method used to exchange two variable of type int
Program class { static void the Main (String [] args) { // exchange method using two variables of type int int N1 = 10; int N2 = 20 is; the Test (REF N1, N2 REF); Console.WriteLine ( "two the value n1 is exchanged {0}, n2 is: {}. 1 ", n1, N2); the Console.ReadKey (); } public static void the Test (REF n1 int, int REF N2) { int TEMP = n1; N2 = N1; N2 = TEMP; } }
Execution code output result, as shown:
to sum up:
Ref parameters can be changed into a variable one method, after the completion of the change, and then change the value with a method.
ref parameter requires the assignment must be outside the method, and the method may not be assigned.
Three, params variable parameters
[Example] seeking arbitrary integer type and the length of the array
class Program { static void Main(string[] args) { //求任意长度数组的和 整数类型的 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; } }
执行代码输出结果,如图所示:
如图所示:
总结:
将实参数列表中跟可变参数数组类型一致的元素都当做数组的元素去处理
params可变参数必须是形参列表中的最后一个元素,并且在方法声明中只允许一个params关键字。
四、out 与ref的区别
区别:
1.使用ref参数时,传入的参数必须被初始化,对out而言,必须在方法中对其完成初始化。
2.使用ref参数和out参数时,在方法的参数和执行方法时,都要加ref或者out关键字。
3.out参数适合用在需要return多个返回值的地方,而ref参数则用在需要被调用的方法修改调用者的引用的时候。
如图所示:
out参数
ref参数
ref关键字是需要先初始化传入的参数,才能够使用。