use the params feature to write functions which accept a variable number of arguments
using System;
public class MainClass {
public static void Main(string[] args) {
int nSum;
Console.WriteLine("{0}", SumArgs(out nSum, 1, 2, 3));
int[] nArray = { 4, 5, 6 };
Console.WriteLine("{0}", SumArgs(out nSum, nArray));
}
public static int SumArgs(out int nSum, params int[] list) {
nSum = 0;
foreach (int n in list) {
nSum += n;
}
return nSum;
}
}
Related examples in the same category