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
Hi, I found this post from 2009 very useful: http://www.phoenixcode.net/post/2009/07/30/Method-Parameters.aspx
Hi Steven – Thanks for visiting and thanks for the link. Its very touching that you review posts from websites whose names include a mythical creature. Does that mean that *gasp* I’m mythical?