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

CHAPTER 5 METHODS

Arrays As Actual Parameters

You can also create and populate an array before the method call and pass the single array variable as the actual parameter. In this case, the compiler uses your array, rather than creating one.

For example, the following code uses method ListInts, declared in the previous example. In this code, Main creates an array and uses the array variable as the actual parameter, rather than using separate integers.

static void Main()

{

int[] myArr = new int[] { 5, 6, 7 }; // Create and initialize array.

MyClass

mc =

new MyClass();

 

 

 

mc.ListInts(myArr);

//

Call method to

print the values.

foreach

(int

x in myArr)

 

 

 

Console.WriteLine("{0}", x);

//

Print out each

element.

}

This code produces the following output:

50

60

70

50

60

70

Summary of Parameter Types

Since there are four parameter types, it’s sometimes difficult to remember their various characteristics. Table 5-2 summarizes them, making it easier to compare and contrast them.

Table 5-2. Summary of Parameter Type Syntactic Usage

Parameter

Modifier

Used at

Used at

Implementation

Type

 

Declaration?

Invocation?

 

 

 

 

 

 

Value

None

 

 

The system copies the value of the actual

 

 

 

 

parameter to the formal parameter.

Reference

ref

Yes

Yes

The formal parameter aliases the actual

 

 

 

 

parameter.

Output

out

Yes

Yes

The formal parameter aliases the actual

 

 

 

 

parameter.

Array

params

Yes

No

This allows passing a variable number of

 

 

 

 

actual parameters to a method.

 

 

 

 

 

96

CHAPTER 5 METHODS

Method Overloading

A class can have more than one method with the same name. This is called method overloading. Each method with the same name must have a different signature than the others.

The signature of a method consists of the following information from the method header of the method declaration:

The name of the method

The number of parameters

The data types and order of the parameters

The parameter modifiers

The return type is not part of the signature—although it’s a common mistake to believe that it is.

Notice that the names of the formal parameters are not part of the signature.

Not part of signature

long AddValues( int a, out int b) { ... }

Signature

For example, the following four methods are overloads of the method name AddValues:

class A

 

 

 

 

{

 

 

 

 

long AddValues( int

a, int

b)

{ return a + b;

}

long AddValues( int

c, int

d, int e)

{ return c + d + e;

}

long AddValues( float

f, float

g)

{ return (long)(f + g); }

long AddValues( long

h, long

m)

{ return h + m;

}

}

 

 

 

 

The following code shows an illegal attempt at overloading the method name AddValues. The two methods differ only on the return types and the names of the formal parameters. But they still have the same signature, because they have the same method name; and the number, types, and order of their parameters are the same. The compiler would produce an error message for this code.

class B

Signature

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

long

AddValues(

long

a, long

b)

{

return

a+b;

}

 

int

AddValues(

long

c, long

d)

{

return

c+d;

}

// Error, same signature

}

 

 

 

 

 

 

 

 

 

Signature

97

CHAPTER 5 METHODS

Named Parameters

So far in our discussion of parameters we’ve used positional parameters, which, as you’ll remember, means that the position of each actual parameter matches the position of the corresponding formal parameter.

Starting with C# 4.0, you can list the actual parameters in your method invocation in any order, as long as you explicitly specify the names of the parameters. The details are the following:

Nothing changes in the declaration of the method. The formal parameters already have names.

In the method invocation, however, you use the formal parameter name, followed by a colon, in front of the actual parameter value or expression, as shown in the following method invocation. Here a, b, and c are the names of the three formal parameters of method Calc:

Actual parameter values

↓ ↓

c.Calc ( c: 2, a: 4, b: 3);

↑ ↑ ↑

Named parameters

Figure 5-11 illustrates the structure of using named parameters.

Figure 5-11. When using named parameters, include the parameter name in the method invocation. No changes are needed in the method declaration.

98

CHAPTER 5 METHODS

You can use both positional and named parameters in an invocation, but all the positional parameters must be listed first. For example, the following code shows the declaration of a method called Calc, along with five different calls to the method using different combinations of positional and named parameters:

class MyClass

{

public int Calc( int a, int b, int c ) { return ( a + b ) * c; }

static void Main()

{

MyClass mc = new MyClass( );

int r0 = mc.Calc( 4, 3, 2 );

 

 

// Positional Parameters

int r1

= mc.Calc( 4, b: 3, c: 2

);

 

// Positional and Named Parameters

int r2

= mc.Calc( 4, c: 2, b: 3

);

 

// Switch order

int

r3

=

mc.Calc(

c: 2,

b: 3, a: 4

);

//

All named parameters

int

r4

=

mc.Calc(

c: 2,

b: 1 + 2, a: 3 + 1 );

//

Named parameter expressions

Console.WriteLine("{0}, {1}, {2}, {3}, {4}", r0, r1, r2, r3, r4);

}

}

This code produces the following output:

14, 14, 14, 14, 14

Named parameters are useful as a means of self-documenting a program, in that they can show, at the position of the method call, what values are being assigned to which formal parameters. For example, in the following two calls to method GetCylinderVolume, the second call is a bit more informative and less prone to error.

class MyClass

{

double GetCylinderVolume( double radius, double height )

{

return 3.1416 * radius * radius * height;

}

static void Main( string[] args )

{

MyClass mc = new MyClass(); double volume;

volume = mc.GetCylinderVolume( 3.0, 4.0 );

...

volume = mc.GetCylinderVolume( radius: 3.0, height: 4.0 );

...

 

 

}

 

More informative

}

99

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