Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 5 METHODS

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 parameters for a particular formal parameter. Important points about parameter arrays are the following:

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, you must 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 inVals can represent zero or more actual int parameters.

Array of ints

 

 

 

 

void ListInts( params int[] inVals )

{ ...

 

 

 

 

 

Modifier

 

Parameter name

The empty set of square brackets after the type name specifies that the parameter will be an array of ints. You don’t need to worry about the details of arrays here. They’re covered in detail in Chapter 14. For our purposes here, though, all you need to know is the following:

An array is an ordered set of data items of the same type.

An array is accessed by using a numerical index.

An array is a reference type and therefore stores all its data items in the heap.

92

CHAPTER 5 METHODS

Method Invocation

You can supply the actual parameters for a parameter array in two ways. The forms you can use are the following:

A comma-separated list of elements of the data type. All the elements must be of the type specified in the method declaration.

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 usage of the modifier in parameter arrays doesn’t 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.

The summary for the usage of the params modifier is the following:

It is required in the declaration.

It is not allowed in the invocation.

Expanded Form

The first form of method invocation, where you use separate actual parameters in the invocation, 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[] inVals ) { ... }

// 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

93

CHAPTER 5 METHODS

When you use an invocation with separate actual parameters for a parameter array, the compiler does the following:

It takes the list of actual parameters and uses them to create and initialize an array in the heap.

It stores the reference to the array in the formal parameter on the stack.

If there are no actual parameters at the position corresponding to the formal parameter array, the compiler creates an array with zero elements and uses that.

For example, the following code declares a method called ListInts, which takes a parameter array. Main declares three ints and passes them to the array.

class MyClass

Parameter array

{

public void ListInts( params int[] inVals )

{

if ( (inVals != null) && (inVals.Length != 0))

for (int i = 0; i < inVals.Length; i++)

// Process the array.

{

 

 

 

inVals[i] = inVals[i] * 10;

 

Console.WriteLine("{0}", inVals[i]);

// Display new value.

}

 

 

 

}

 

 

 

}

 

 

 

class Program

 

{

 

 

 

static void Main()

 

{

 

 

 

int first = 5, second = 6, third = 7;

// Declare three ints.

MyClass mc = new MyClass();

 

mc.ListInts( first, second, third );

// Call the method.

 

 

 

 

Actual parameters

Console.WriteLine("{0}, {1}, {2}", first, second, third);

}

}

This code produces the following output:

50

60

70 5, 6, 7

94

CHAPTER 5 METHODS

Figure 5-10 illustrates the following about the values of the actual and formal parameters at various stages in the execution of the method:

Before the method call, the three actual parameters are already on the stack.

By the beginning of the method, the three actual parameters have been used to initialize an array in the heap, and the reference to the array has been assigned to formal parameter inVals.

Inside the method, the code first checks to make sure the array reference is not null and then processes the array by multiplying each element in the array by 10 and storing it back.

After method execution, the formal parameter, inVals, is out of scope.

Figure 5-10. Parameter array

An important thing to remember about parameter arrays is that when an array is created in the heap, the values of the actual parameters are copied to the array. In this way, they’re like value parameters.

If the array parameter is a value type, the values are copied, and the actual parameters cannot be affected inside the method.

If the array parameter is a reference type, the references are copied, and the objects referenced by the actual parameters can be affected inside the method.

95

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]