Information Technology Reference
In-Depth Information
Method Invocation
You can supply the actual parameters in two ways. The forms you can use are the following:
A comma-separated list of elements of the data type
ListInts( 10, 20, 30 ); // Three ints
A one-dimensional array of elements of the data type
int[] IntArray = {1, 2, 3};
ListInts( IntArray ); // An array variable
Notice in these examples that you do not use the
params
modifier in the invocation. The
use of the modifier in parameter arrays does not fit the pattern of the other parameter types.
The other parameter types are consistent, in that they either use a modifier or do not use
a modifier.
-
Value parameters take
no
modifier in
either
the declaration or the invocation.
-
Reference and output parameters require the modifier in
both
places.
Parameter arrays, however,
-
Require the modifier in the declaration
-
Do not accept it in the invocation
Expanded Form
You will start by examining the form where you use separate actual parameters in the invoca-
tion. This is sometimes called the
expanded form
.
For example, the declaration of method
ListInts
in the following code matches all the
method invocations below it, even though they have different numbers of actual parameters.
void ListInts( params int[] InputList ) { ... } // Method declaration
...
ListInts( ); // 0 actual parameters
ListInts( 1, 2, 3 ); // 3 actual parameters
ListInts( 4, 5, 6, 7 ); // 4 actual parameters
ListInts( 8, 9, 10, 11, 12 ); // 5 actual parameters