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

CHAPTER 5 METHODS

Local Constants

A local constant is much like a local variable, except that once it is initialized, its value can’t be changed. Like a local variable, a local constant must be declared inside a block.

The two most important characteristics of a constant are the following:

A constant must be initialized at its declaration.

A constant cannot be changed after its declaration.

The core declaration for a constant is shown following. The syntax is the same as that of a field or variable declaration, except for the following:

The addition of the keyword const before the type.

The mandatory initializer. The initializer value must be determinable at compile time and is usually one of the predefined simple types or an expression made up of them. It can also be the null reference, but it cannot be a reference to an object, because references to objects are determined at run time.

Note The keyword const is not a modifier but part of the core declaration. It must be placed immediately before the type.

Keyword

const Type Identifier = Value;

Initializer required

A local constant, like a local variable, is declared in a method body or code block, and it goes out of scope at the end of the block in which it is declared. For example, in the following code, local constant PI goes out of scope at the end of method DisplayRadii.

void DisplayRadii()

 

{

 

const double PI = 3.1416;

// Declare local constant

for (int radius = 1; radius <= 5; radius++)

 

{

 

double area = radius * radius * PI;

// Read from local constant

Console.WriteLine

("Radius: {0}, Area: {1}" radius, area);

}

}

73

CHAPTER 5 METHODS

Flow of Control

Methods contain most of the code for the actions that comprise a program. The remainder is in other function members, such as properties and operators—but the bulk is in methods.

The term flow of control refers to the flow of execution through your program. By default, program execution moves sequentially from one statement to the next. The control statements allow you to modify the order of execution.

In this section, I’ll just mention some of the available control statements you can use in your code. Chapter 9 covers them in detail.

Selection statements: These statements allow you to select which statement, or block of statements, to execute.

if: Conditional execution of a statement

if...else: Conditional execution of one statement or another

switch: Conditional execution of one statement from a set

Iteration statements: These statements allow you to loop, or iterate, on a block of statements.

for: Loop—testing at the top

while: Loop—testing at the top

do: Loop—testing at the bottom

foreach: Execute once for each member of a set

Jump statements: These statements allow you to jump from one place in the block or method to another.

break: Exit the current loop.

continue: Go to the bottom of the current loop.

goto: Go to a named statement.

return: Return execution to the calling method.

For example, the following method shows two of the flow-of-control statements. Don’t worry about the details.

void SomeMethod()

{

int intVal = 3;

Equality comparison operator

 

 

 

 

 

if( intVal ==

3 )

//

if statement

Console.WriteLine("Value is 3. ");

 

 

for( int i=0;

i<5; i++ )

//

for statement

Console.WriteLine("Value of i: {0}", i);

 

 

}

74

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