1.当希望方法返回多个值时,声明out 方法很有用。
这样使方法可以有选择地返回值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 求数组最大最小值 { class Program { static void Main(string[] args) { int[] numbers = { 45, 25, 14, 54, 85, 45, 4, 15, 47 }; int myMax, myMin; myMax = FindMaxMin(numbers, out myMin); Console.WriteLine("max={0} min={1}", myMax, myMin); Console.ReadKey(); } static int FindMaxMin(int[] numbers, out int min) { int max; max = min = numbers[0]; for (int i = 1; i < numbers.Length; i++) { if (numbers[i] > max) { max = numbers[i]; } if (numbers[i] < min) { min = numbers[i]; } } return max; } } }