Information Technology Reference
In-Depth Information
Parameter Arrays
In the parameter types I've covered so far, there must be exactly one actual parameter for each
formal parameter.
Parameter arrays
are different in that they allow
zero or more
actual param-
eters for a particular formal parameter.
There can be only one parameter array in a parameter list. If there is one, it must be the last
parameter in the list. To declare a parameter array, do the following:
Use the
params
modifier before the data type.
Place a set of empty square brackets after the data type.
The following method header shows the syntax for the declaration of a parameter array of
type
int
. In this example, formal parameter
InputList
can represent zero or more actual
int
parameters.
Array of ints
↓
void ListInts( params int[] InputList )
{ ...
↑
↑
Modifier
Parameter name
The empty set of square brackets after the type name specifies that the parameter will be
an
array
of
int
s. You don't need to worry about the details of arrays here. They are covered in
detail in Chapter 14. For our purposes here, though, all you need to know is that an array is
An ordered set of data items of the same type
Accessed by using a numerical index
A reference type, and therefore stores all its data items in the heap