2.15. Variable-Length Parameter Lists

Our message() function of the preceding section is actually something of a problem. It represents the collection of values a user may wish to display. The problem is that the type and number of these values of interest to users are limitless. Trying to accommodate even a subset of all the requested variations soon begins to drive us crazy.

A more manageable solution is to declare a signature that can be passed an arbitrary number of arguments of possibly arbitrary types. We do this through the params variable-length parameter, which is characterized as follows:

  • The params keyword identifies that we are passing a variable number of parameters.

  • An array of the type of arguments we wish to accept follows the ...

Get C# Primer: A Practical Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.