1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication7 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 int sum1;14 int[] intArray = { 1,2,3};15 sum1 = Test(intArray);16 Console.WriteLine("数组的和是:{0}",sum1);17 Console.ReadKey();18 }19 public static int Test(int[] numbers)20 {21 int sum = 0;22 for (int i = 0; i < numbers.Length; i++)23 {24 sum += numbers[i];25 }26 return sum;27 }28 }29 }
如果参数只是传入数组的个别元素,则可以这个样写:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication7 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 int sum1;14 int[] intArray = { 1,2,3};15 sum1 = Test(intArray[0],intArray[2]);16 Console.WriteLine("数组的和是:{0}",sum1);17 Console.ReadKey();18 }19 public static int Test(int a, int b)20 {21 int sum = a + b;22 return sum;23 }24 }25 }