C#: Method params keyword

Please see my other C# articles.

Using params to receive unknown quantity of arguments

In this article, I’ll demonstrate how to configure a method signature to receive a amount of arguments unknown at design time.

I’ll begin with an array of lottery picks I want to display on the screen.

Now, I want a method which receives these arguments, but don’t want to have to specify a parameter for each argument. Also, I want the ability to extend this method in the future to support a greater or lesser quantity of arguments, without method overloading.

I’ll create this method like I normally would, but utilize the params keyword for the only parameter which instructs the method signature to receive an unknown quantity of arguments, in this case, int values.

Note: When using params, all arguments passed must be of the same data type.

Now, I may call this method, passing in however many arguments I desire.

Now, I want to overload this method to I can specify the first pick differently, which represents the lottery multiplier.

Note: When using additional parameters when using params, the params array should be listed last in the signature.