C params keyword.
Here is an example:
public int SumThemAll(params int[] numbers) { return numbers.Sum(); } public void SumThemAllAndPrintInString(string s, params int[] numbers) { Console.WriteLine(string.Format(s, SumThemAll(numbers))); } public void MyFunction() { int result = SumThemAll(2, 3, 4, 42); SumThemAllAndPrintInString("The result is: {0}", 1, 2, 3); }
The code shows various things. First of all, the argument with the params should always be the last (and there can only be one function). Alternatively, you can call a function that takes params two ways. The first path is illustrated in the first line of MyFunction , where each number is added as one argument. However, it can also be called using an array, as shown in SumThemAllAndPrintInString , which calls SumThemAll with an int[] called numbers .
Klaus Byskov Pedersen Mar 12 '10 at 20:45 2010-03-12 20:45
source share