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

CHAPTER 5 METHODS

Output Parameters

Output parameters are used to pass data from inside the method back out to the calling code. Their behavior is very similar to reference parameters. Like reference parameters, output parameters have the following requirements:

You must use a modifier in both the method declaration and the invocation. With output parameters, the modifier is out, rather than ref.

Like reference parameters, the actual parameter must be a variable—it cannot be another type of expression. This makes sense, since the method needs a memory location to store the value it’s returning.

For example, the following code declares a method called MyMethod, which takes a single output parameter.

out modifier

 

void MyMethod( out int val )

// Method declaration

{ ... }

 

...

 

int y = 1;

// Variable for the actual parameter

MyMethod ( out y );

// Method call

 

out modifier

 

Like reference parameters, the formal parameters of output parameters act as if they were aliases for the actual parameters. Any changes made to a formal parameter inside the method are visible through the actual parameter variable after the method completes execution.

Unlike reference parameters, output parameters require the following:

Inside the method, an output parameter must be assigned to before it can be read from. This means that the initial values of the parameters are irrelevant and that you don’t have to assign values to the actual parameters before the method call.

Inside the method, every possible path through the code must assign a value to every output parameter before the method exits.

89

CHAPTER 5 METHODS

Since the code inside the method must write to an output parameter before it can read from it, it is impossible to send data into a method using output parameters. In fact, if there is any execution path through the method that attempts to read the value of an output parameter before the method has assigned it a value, the compiler produces an error message.

public

void

Add2( out int outValue )

{

int

var1

= outValue + 2; //

Error! Can't read from an output parameter

 

}

 

 

//

before it has been assigned to by the method.

For example, the following code again shows method MyMethod, but this time using output parameters:

class MyClass

 

 

 

{

 

 

 

public int Val = 20;

 

// Initialize field to 20.

}

 

 

 

class Program

out modifier

out modifier

{

 

static void MyMethod(out MyClass f1, out int f2)

{

 

 

 

f1 = new MyClass();

// Create an object of the class.

f1.Val = 25;

 

// Assign to the class field.

f2

= 15;

 

// Assign to the int param.

}

 

 

 

static void Main()

 

 

{

 

 

 

MyClass a1 = null;

 

int a2;

 

 

 

MyMethod(out a1, out a2);

// Call the method.

}

 

}

out modifiers

 

90

CHAPTER 5 METHODS

Figure 5-9 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, variables a1 and a2, which will be used as the actual parameters, are already on the stack.

At the beginning of the method, the names of the formal parameters are set as aliases for the actual parameters. You can think of variables a1 and f1 as if they referred to the same memory location, and you can think of a2 and f2 as if they referred to the same memory location. The names a1 and a2 are out of scope and cannot be accessed from inside MyMethod.

Inside the method, the code creates an object of type MyClass and assigns it to f1. It then assigns a value to f1’s field and also assigns a value to f2. The assignments to f1 and f2 are both required, since they’re output parameters.

After method execution, the names of the formal parameters are out of scope, but the values of both a1, the reference type, and a2, the value type, have been changed by the activity in the method.

Figure 5-9. With an output parameter, the formal parameter behaves as if it were an alias for the actual parameter, but with the additional requirement that it must be assigned to inside the method.

91

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