czC#02
1、out参数
out参数要求在方法的内部必须为其赋值
using System; using System.Text; namespace Demo { class Program { //返回一个数组的最大值,最小值,均值,长度,名称 static void Main() { int max = 0; int min = 0; int ave = 0; int len = 0; string name = ""; int[] arr = { 1, 4, 10 }; Test(arr, out max, out min, out ave, out len, out name); Console.WriteLine(max); Console.WriteLine(min); Console.WriteLine(ave); Console.WriteLine(len); Console.WriteLine(name); Console.ReadLine(); } public static void Test(int[] arr,out int max, out int min, out int ave, out int len, out string name) { //伪代码 max = 10; min = 1; ave = 5; len = 3; name = "arr"; } } }
2、ref参数
值传递时,原值也会改变
using System; using System.Text; namespace Demo { class Program { static void Main() { int num = 10; Test(ref num); Console.WriteLine(num); Console.ReadLine(); } public static void Test(ref int num) { num += 10; } } }
3. params可变参数
参数列表变为数组,方法执行时数组内的元素不可改变
using System; using System.Text; namespace Demo { class Program { static void Main() { Console.WriteLine(Test("Linda", 100, 90, 98, 94)); Console.ReadLine(); } public static int Test(string name,params int[] score) { return score[0]; } } }