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

CHAPTER 5 METHODS

Value Parameters

There are several kinds of parameters, which pass data to and from the method in slightly different ways. The kind we’ve looked at so far is the default type and is called a value parameter.

When you use value parameters, data is passed to the method by copying the value of the actual parameter to the formal parameter. When a method is called, the system does the following:

It allocates space on the stack for the formal parameters.

It copies the values of the actual parameters to the formal parameters.

An actual parameter for a value parameter doesn’t have to be a variable. It can be any expression evaluating to the matching data type. For example, the following code shows two method calls. In the first, the actual parameter is a variable of type float. In the second, it’s an expression that evaluates to float.

float func1( float

val )

// Declare the method.

{

 

 

 

 

 

 

 

Float data type

 

 

float j = 2.6F;

 

Variable of type float

 

 

float k = 5.1F;

 

 

 

 

 

 

 

 

float fValue1 =

func1( k );

// Method call

 

float fValue2 =

func1( (k + j) / 3 );

// Method call

 

...

 

 

 

 

 

Expression that evaluates to a float

Before you can use a variable as an actual parameter, that variable must have been assigned a value (except in the case of output parameters, which I’ll cover shortly). For reference types, the variable can be assigned either an actual reference or null.

Note Chapter 3 covered value types, which, as you will remember, are types that contain their own data. Don’t be confused that I’m now talking about value parameters. They’re entirely different. Value parameters are parameters where the value of the actual parameter is copied to the formal parameter.

83

CHAPTER 5 METHODS

For example, the following code shows a method called MyMethod, which takes two parameters—a variable of type MyClass and an int.

The method adds 5 to both the int type field belonging to the class and to the int.

You might also notice that MyMethod uses the modifier static, which I haven’t explained yet. You can ignore it for now. I’ll explain static methods in Chapter 6.

class MyClass

 

 

 

 

 

 

{

 

 

 

 

 

 

public int Val = 20;

// Initialize the field to 20.

}

 

 

 

 

 

 

class Program

 

 

 

Formal parameters

 

{

 

 

 

 

 

static void MyMethod( MyClass f1, int f2 )

 

{

 

 

 

 

 

 

f1.Val = f1.Val + 5;

// Add 5 to field of f1 param.

f2

= f2 + 5;

// Add 5 to second param.

}

 

 

 

 

 

 

static void Main( )

 

{

 

 

 

 

 

 

MyClass

a1 = new MyClass();

 

int

a2 = 10;

 

MyMethod( a1, a2 );

// Call the method.

}

 

 

 

}

Actual parameters

 

84

CHAPTER 5 METHODS

Figure 5-7 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.

By the beginning of the method, the system has allocated space on the stack for the formal parameters and copied the values from the actual parameters.

Since a1 is a reference type, the reference is copied, resulting in both the actual and formal parameters referring to the same object in the heap.

Since a2 is a value type, the value is copied, producing an independent data item.

At the end of the method, both f2 and the field of object f1 have been incremented by 5.

After method execution, the formal parameters are popped off the stack.

The value of a2, the value type, is unaffected by the activity in the method.

The value of a1, the reference type, however, has been changed by the activity in the method.

Figure 5-7. Value parameters

85

CHAPTER 5 METHODS

Reference Parameters

The second type of parameter is called a reference parameter.

When using a reference parameter, you must use the ref modifier in both the declaration and the invocation of the method.

The actual parameter must be a variable, and it must have been assigned to before being used as the actual parameter. If it’s a reference type variable, it can be assigned either an actual reference or the value null.

For example, the following code illustrates the syntax of the declaration and invocation:

Include the ref modifier.

 

 

void MyMethod( ref int val )

// Method declaration

{ ... }

 

 

 

int y = 1;

// Variable for the actual parameter

MyMethod ( ref y );

// Method call

 

Include the ref modifier.

 

MyMethod ( ref 3+5 );

// Error!

 

 

 

Must use a variable

 

In the previous section you saw that for value parameters, the system allocates memory on the stack for the formal parameters. In contrast, for reference parameters:

The formal parameter name acts as if it were an alias for the actual parameter variable; that is, it acts as if it referred to the same memory location.

Since the formal parameter name and the actual parameter name are acting as if they reference the same memory location, clearly any changes made to the formal parameter during method execution are visible after the method is completed, through the actual parameter variable.

Note Remember to use the ref keyword in both the method declaration and the invocation.

86

CHAPTER 5 METHODS

For example, the following code shows method MyMethod again, but this time the parameters are reference parameters rather than value parameters:

class MyClass

{

public int Val = 20;

 

// Initialize field to 20.

}

 

 

 

class Program

ref modifier

ref modifier

{

 

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

{

 

 

 

f1.Val = f1.Val + 5;

// Add 5 to field of f1 param.

f2

= f2 + 5;

 

// Add 5 to second param.

}

 

 

 

static void Main()

 

 

{

 

 

 

MyClass a1 = new MyClass();

 

int a2

= 10;

 

 

MyMethod(ref a1, ref a2);

// Call the method.

}

 

}

ref modifiers

 

87

CHAPTER 5 METHODS

Figure 5-8 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.

By the beginning of the method, the names of the formal parameters have been set as if they were aliases for the actual parameters. You can think of variables a1 and f1 as if they referred to the same memory location and a2 and f2 as if they referred to the same memory location.

At the end of the method, both f2 and the field of the object of f1 have been incremented by 5.

After method execution, the names of the formal parameters are gone (“out of scope”), but both the value of a2, which is the value type, and the value of the object pointed at by a1, which is the reference type, have been changed by the activity in the method.

Figure 5-8. With a reference parameter, the formal parameter behaves as if it were an alias for the actual

parameter.

88

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