To create a method that can take unlimited parameters, utilize the params keyword in the method declaration. This allows you to specify a method parameter that take an argument where the number of arguments is variable. You are not allowed to have any other method parameters declared after a params method parameter.
MSDN resource: params
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
static void Main(string[] args) { AddNumbers(12, 14, 15); } public static void AddNumbers(params int[] numbers) { int result = 0; for (int i = 0; i < numbers.Length; i++) result+= numbers[i]; Console.WriteLine(result); } |
PSGMUMGGBJAR