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

CHAPTER 5 METHODS

Optional Parameters

Another feature introduced in C# 4.0, is called optional parameters. An optional parameter is a parameter that you can either include or omit when invoking the method.

To specify that a parameter is optional, you need to include a default value for that parameter in the method declaration. The syntax for specifying the default value is the same as that of initializing a local variable, as shown in the method declaration of the following code. In this example:

Formal parameter b is assigned the default value 3.

Therefore, if the method is called with only a single parameter, the method will use the value 3 as the initial value of the second parameter.

class MyClass

Optional Parameter

{

 

 

public int Calc( int a, int b = 3 )

{

 

 

 

 

 

 

 

 

 

 

return a

+ b;

Default Value Assignment

}

 

 

 

 

 

 

static

void

Main()

 

 

 

{

 

 

 

 

 

 

MyClass mc = new MyClass();

 

 

int

r0

=

mc.Calc( 5, 6 );

 

// Use explicit values.

int

r1

=

mc.Calc( 5 );

 

// Use default for b.

Console.WriteLine( "{0}, {1}", r0, r1 );

}

}

This code produces the following output:

11, 8

100

CHAPTER 5 METHODS

There are several important things to know about declaring optional parameters:

Not all types of parameters can be used as optional parameters.

You can use value types as optional parameters as long as the default value is determinable at compile time.

You can only use a reference type as an optional parameter if the default value is null.

Figure 5-12. Optional parameters can only be value parameter types.

All required parameters must be declared before any optional parameters are declared. If there is a params parameter, it must be declared after all the optional parameters. Figure 5-13 illustrates the required syntactic order.

Figure 5-13. In the method declaration, optional parameters must be declared after all the required

parameters and before the params parameter, if one exists.

101

CHAPTER 5 METHODS

As you saw in the previous example, you use the default value of an optional parameter by leaving out the corresponding actual parameter from the method invocation. You can’t, however, omit just any combination of optional parameters because in many situations it would be ambiguous as to which optional parameters to use. The rules are the following:

You must omit parameters starting from the end of the list of optional parameters and work toward the beginning.

That is, you can omit the last optional parameter, or the last n optional parameters, but you can’t pick and choose to omit any arbitrary optional parameters; they must be taken off the end.

class MyClass

{

public int Calc( int a = 2, int b = 3, int c = 4 )

{

return (a + b) * c;

}

 

 

 

 

 

static

void

Main( )

 

 

{

 

 

 

 

 

MyClass mc = new MyClass( );

 

int

r0

=

mc.Calc( 5, 6, 7 );

// Use all explicit values.

int

r1

=

mc.Calc( 5, 6

);

// Use default for c.

int

r2

=

mc.Calc( 5 );

 

// Use default for b and c.

int

r3

=

mc.Calc( );

 

// Use all defaults.

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

}

}

This code produces the following output:

77, 44, 32, 20

102

CHAPTER 5 METHODS

To omit optional parameters from arbitrary positions within the list of optional parameters, rather than from the end of the list, you must use the names of the optional parameters to disambiguate the assignments. You are therefore using both the named parameters and optional parameters features, as illustrated in the following code:

class MyClass

{

double GetCylinderVolume( double radius = 3.0, double height = 4.0 )

{

return 3.1416 * radius * radius * height;

}

static void

Main( )

 

 

{

 

 

 

MyClass mc = new MyClass();

 

 

double volume;

 

 

volume =

mc.GetCylinderVolume(

3.0, 4.0 );

// Positional

Console.WriteLine( "Volume = "

+ volume );

 

volume =

mc.GetCylinderVolume(

radius: 2.0 );

// Use default height

Console.WriteLine( "Volume = "

+ volume );

 

volume =

mc.GetCylinderVolume(

height: 2.0 );

// Use default radius

Console.WriteLine( "Volume = "

+ volume );

 

volume =

mc.GetCylinderVolume(

);

// Use both defaults

Console.WriteLine( "Volume = "

+ volume );

 

}

 

 

 

}

This code produces the following output:

Volume = 113.0976

Volume = 50.2656

Volume = 56.5488

Volume = 113.0976

103

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