out参数
与c++的引用的对比
out参数可以用来传递方法返回值,与c++中的引用有点像,但是还有有些不同:
- 调用方法的时候必须写out参数
- 调用方法之前必须先分配空间
- 调用方法之前不用先赋值.
- 必须在方法内部对out参数赋值;
下面自己实现一个tryparse函数
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace out参数 8 { 9 class Program 10 { 11 public struct Mid 12 { 13 public int[] _num; 14 public string _name; 15 } 16 static void Main(string[] args) 17 { 18 //写一个方法,求一个数组中的最大值、最小值、总和、平均值。 19 int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 20 //将要返回的四个值,放到一个数组里返回;但是问题是数组是一组相同类型的数据,所以才能放到一个数组里面。 21 //如果不同类型的话,只能从参数返回了.这时候又可以用结构封装起来返回;但是这个结构不一定是有意义的,同时是需要做后续操作的. 22 // Mid mid = GetMaxMinSumAvg(numbers); 23 // Console.WriteLine("名字是{0}",mid._name); 24 int max, min, sum; 25 double avg; 26 GetTest(numbers, out max, out min, out sum,out avg); 27 Console.WriteLine("最大值是{0},最小值是{1},总和为{2},平均值为{3}",max,min,sum,avg); 28 Console.ReadKey(); 29 30 } 31 32 /// <summary> 33 /// 计算一个整数数组的最大值,最小值,平均值,总和,平均值; 34 /// </summary> 35 /// <param name="nums">需要计算的那个数组</param> 36 /// <param name="max">多余返回的最大值</param> 37 /// <param name="min">多余返回的最小值</param> 38 /// <param name="sum">对于返回的总和</param> 39 /// <param name="avg">多余返回的平均值</param> 40 public static void GetTest(int []nums,out int max,out int min,out int sum,out double avg) 41 { 42 //必须在方法内部对out参数赋值; 43 max = nums[0]; 44 min = nums[0]; 45 sum = nums[0]; 46 for (int i = 1; i < nums.Length; i++) 47 { 48 if (nums[i]>max) 49 { 50 max = nums[i]; 51 } 52 if (nums[i]<min) 53 { 54 min = nums[i]; 55 } 56 sum += nums[i]; 57 } 58 avg = sum * 0.1 / nums.Length; 59 60 } 61 /// <summary> 62 /// 自己实现一个int型的TryParse()函数; 63 /// </summary> 64 /// <param name="str"></param> 65 /// <param name="num"></param> 66 /// <returns></returns> 67 public static bool MyTryParse(string str,out int num) 68 { 69 try 70 { 71 num = int.Parse(str); 72 } 73 catch (OverflowException) 74 { 75 76 num = 0; 77 return false; 78 } 79 return true; 80 } 81 } 82 }
ref参数
ref就相当于c++中的引用;比out要更像一点,看它就像是reference的缩写;
注意三点:
- 在调用方法前一定要赋值;
- 调用时一定要写ref;
- 一定要构造空间
下面写一个交换两个变量内容的操作:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 //ref就相当于c++中的引用;比out要更像一点,out专注返回20年; 8 namespace ref参数 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 int n1 = 10; 15 int n2 = 20; 16 //注意两点;在调用方法前一定要赋值;一定要写ref;一定要构造空间 17 Exchange(ref n1, ref n2); 18 Console.WriteLine("交换后,第一个变量是{0},第二个参数是{1}",n1,n2); 19 Console.ReadKey(); 20 21 } 22 /// <summary> 23 /// 交换两个变量 24 /// </summary> 25 /// <param name="n1">第一个变量</param> 26 /// <param name="n2">第二个变量</param> 27 public static void Exchange(ref int n1,ref int n2) 28 { 29 n1 = n1 - n2; 30 n2 = n1 + n2; 31 n1 = n2 - n1; 32 } 33 } 34 }
params参数
params是可变类型参数,但是它与c++中的变长参数又有不同:
- 将实参列表中跟可变参数数组类型一致的元素都当做数组的元素去处理。
- params可变参数必须是形参列表中的最后一个元素。
- 唯一性:在一个函数里,只能有一个params可变参数
- 调用时有两种传递方式:数组和直接数据传;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Params参数 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int [] a={1,2,3}; 14 Test("呵呵",a); 15 } 16 public static void Test(string name,params int[] par) 17 { 18 19 } 20 } 21 } 22 23 /* 24 * param可变参数 可以没有 25 * 唯一性 26 * 最后性 27 * 两种传递方式 28 * 求任意长度数组的最大值; 29 * 30 */