Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

 

C# LANGUAGE SPECIFICATION

1

void DoCommand(string command) {

2

switch (command.ToLower()) {

3

case "run":

4

DoRun();

5

break;

6

case "save":

7

DoSave();

8

break;

9

case "quit":

10

DoQuit();

11

break;

12

default:

13

InvalidCommand(command);

14

break;

15}

16}

17end example]

18[Note: Like the string equality operators (§14.9.7), the switch statement is case sensitive and will execute a

19given switch section only if the switch expression string exactly matches a case label constant. end note]

20When the governing type of a switch statement is string, the value null is permitted as a case label

21constant.

22The statement-lists of a switch-block can contain declaration statements (§15.5). The scope of a local

23variable or constant declared in a switch block is the switch block.

24Within a switch block, the meaning of a name used in an expression context shall always be the same

25(§14.5.2.1).

26The statement list of a given switch section is reachable if the switch statement is reachable and at least

27one of the following is true:

28The switch expression is a non-constant value.

29The switch expression is a constant value that matches a case label in the switch section.

30The switch expression is a constant value that doesn’t match any case label, and the switch section

31contains the default label.

32A switch label of the switch section is referenced by a reachable goto case or goto default

33statement.

34The end point of a switch statement is reachable if at least one of the following is true:

35The switch statement contains a reachable break statement that exits the switch statement.

36The switch statement is reachable, the switch expression is a non-constant value, and no default

37label is present.

38The switch statement is reachable, the switch expression is a constant value that doesn’t match any

39case label, and no default label is present.

4015.8 Iteration statements

41Iteration statements repeatedly execute an embedded statement.

42iteration-statement:

43

44

45

46

while-statement do-statement for-statement foreach-statement

4715.8.1 The while statement

48The while statement conditionally executes an embedded statement zero or more times.

224

Chapter 15 Statements

1

while-statement:

2

while ( boolean-expression ) embedded-statement

3A while statement is executed as follows:

4The boolean-expression (§14.16) is evaluated.

5If the boolean expression yields true, control is transferred to the embedded statement. When and if

6control reaches the end point of the embedded statement (possibly from execution of a continue

7statement), control is transferred to the beginning of the while statement.

8If the boolean expression yields false, control is transferred to the end point of the while statement.

9Within the embedded statement of a while statement, a break statement (§15.9.1) can be used to transfer

10control to the end point of the while statement (thus ending iteration of the embedded statement), and a

11continue statement (§15.9.2) can be used to transfer control to the end point of the embedded statement

12(thus performing another iteration of the while statement).

13The embedded statement of a while statement is reachable if the while statement is reachable and the

14boolean expression does not have the constant value false.

15The end point of a while statement is reachable if at least one of the following is true:

16The while statement contains a reachable break statement that exits the while statement.

17The while statement is reachable and the boolean expression does not have the constant value true.

1815.8.2 The do statement

19The do statement conditionally executes an embedded statement one or more times.

20do-statement:

21

do embedded-statement while ( boolean-expression ) ;

22A do statement is executed as follows:

23Control is transferred to the embedded statement.

24When and if control reaches the end point of the embedded statement (possibly from execution of a

25continue statement), the boolean-expression (§14.16) is evaluated. If the boolean expression yields

26true, control is transferred to the beginning of the do statement. Otherwise, control is transferred to the

27end point of the do statement.

28Within the embedded statement of a do statement, a break statement (§15.9.1) can be used to transfer

29control to the end point of the do statement (thus ending iteration of the embedded statement), and a

30continue statement (§15.9.2) can be used to transfer control to the end point of the embedded statement

31(thus performing another iteration of the do statement).

32The embedded statement of a do statement is reachable if the do statement is reachable.

33The end point of a do statement is reachable if at least one of the following is true:

34The do statement contains a reachable break statement that exits the do statement.

35The end point of the embedded statement is reachable and the boolean expression does not have the

36constant value true.

3715.8.3 The for statement

38The for statement evaluates a sequence of initialization expressions and then, while a condition is true,

39repeatedly executes an embedded statement and evaluates a sequence of iteration expressions.

40for-statement:

41

for ( for-initializeropt ; for-conditionopt ; for-iteratoropt ) embedded-statement

225

 

C# LANGUAGE SPECIFICATION

1

for-initializer:

2

local-variable-declaration

3

statement-expression-list

4

for-condition:

5

boolean-expression

6

for-iterator:

7

statement-expression-list

8

statement-expression-list:

9

statement-expression

10

statement-expression-list , statement-expression

11The for-initializer, if present, consists of either a local-variable-declaration (§15.5.1) or a list of statement-

12expressions (§15.6) separated by commas. The scope of a local variable declared by a for-initializer starts at

13the local-variable-declarator for the variable and extends to the end of the embedded statement. The scope

14includes the for-condition and the for-iterator.

15The for-condition, if present, shall be a boolean-expression (§14.16).

16The for-iterator, if present, consists of a list of statement-expressions (§15.6) separated by commas.

17A for statement is executed as follows:

18If a for-initializer is present, the variable initializers or statement expressions are executed in the order

19they are written. This step is only performed once.

20If a for-condition is present, it is evaluated.

21If the for-condition is not present or if the evaluation yields true, control is transferred to the embedded

22statement. When and if control reaches the end point of the embedded statement (possibly from

23execution of a continue statement), the expressions of the for-iterator, if any, are evaluated in

24sequence, and then another iteration is performed, starting with evaluation of the for-condition in the

25step above.

26If the for-condition is present and the evaluation yields false, control is transferred to the end point of

27the for statement.

28Within the embedded statement of a for statement, a break statement (§15.9.1) can be used to transfer

29control to the end point of the for statement (thus ending iteration of the embedded statement), and a

30continue statement (§15.9.2) can be used to transfer control to the end point of the embedded statement

31(thus executing another iteration of the for statement).

32The embedded statement of a for statement is reachable if one of the following is true:

33The for statement is reachable and no for-condition is present.

34The for statement is reachable and a for-condition is present and does not have the constant value

35false.

36The end point of a for statement is reachable if at least one of the following is true:

37The for statement contains a reachable break statement that exits the for statement.

38The for statement is reachable and a for-condition is present and does not have the constant value

39true.

4015.8.4 The foreach statement

41The foreach statement enumerates the elements of a collection, executing an embedded statement for each

42element of the collection.

43foreach-statement:

44

foreach ( type identifier in expression ) embedded-statement

226

Chapter 15 Statements

1The type and identifier of a foreach statement declare the iteration variable of the statement. The iteration

2variable corresponds to a read-only local variable with a scope that extends over the embedded statement.

3During execution of a foreach statement, the iteration variable represents the collection element for which

4an iteration is currently being performed. A compile-time error occurs if the embedded statement attempts to

5modify the iteration variable (via assignment or the ++ and -- operators) or pass the iteration variable as a

6ref or out parameter.

7The compile-time processing of a foreach statement first determines the collection type, enumerator type

8and element type of the expression. This determination proceeds as follows:

9If the type X of expression is an array type then there is an implicit reference conversion from X to the

10System.Collections.IEnumerable interface (since System.Array implements this interface).

11The collection type is the System.Collections.IEnumerable interface, the enumerator type is the

12System.Collections.IEnumerator interface and the element type is the element type of the array

13type X.

14Otherwise, determine whether the type X has an appropriate GetEnumerator method:

15o Perform member lookup on the type X with identifier GetEnumerator and no type arguments. If

16the member lookup does not produce a match or produces an ambiguity or produces a match that is

17not a method group, check for an enumerable interface as described below. It is recommended that a

18warning be issued if member lookup produces anything except a method group or no match.

19o Perform overload resolution using the resulting method group and an empty argument list. If

20overload resolution results in no applicable methods or results in an ambiguity or results in a single

21best method but that method is either static or not public, check for an enumerable interface as

22described below. It is recommended that a warning be issued if overload resolution produces

23anything except an unambiguous public instance method or no applicable methods.

24o If the return type E of the GetEnumerator method is not a class, struct or interface type, an error is

25produced and no further steps are taken.

26o Member lookup is performed on E with the identifier Current and no type arguments. If the

27member lookup produces no match or the result is an error or the result is anything except a public

28instance property that permits reading, an error is produced and no further steps are taken.

29o Member lookup is performed on E with the identifier MoveNext and no type arguments. If the

30member lookup produces no match or the result is an error or the result is anything except a method

31group, an error is produced and no further steps are taken.

32o Overload resolution is performed on the method group with an empty argument list. If overload

33resolution results in no applicable methods or results in an ambiguity or results in a single best

34method but that method is either static or not public, or its return type is not bool, an error is

35produced and no further steps are taken.

36o The collection type is X, the enumerator type is E, and the element type is the type of the Current

37property.

38Otherwise, check for an enumerable interface:

39o If there is exactly one type T such that there is an implicit conversion from X to the interface

40System.Collections.Generic.IEnumerable<T>, then the collection type is this interface,

41the enumerator type is the interface System.Collections.Generic.IEnumerator<T>, and

42the element type is T.

43o Otherwise, if there is more than one such type T, then an error is produced and no further steps are

44taken.

45o Otherwise, if there is an implicit conversion from X to the System.Collections.IEnumerable

46interface, then the collection type is this interface, the enumerator type is the interface

47System.Collections.IEnumerator, and the element type is object.

227

C# LANGUAGE SPECIFICATION

1o Otherwise, an error is produced and no further steps are taken.

2The above steps, if successful, unambiguously produce a collection type C, enumerator type E and element

3type T. A foreach statement of the form

4foreach (V v in x) embedded-statement

5is then expanded to:

6{

7

E e = ((C)(x)).GetEnumerator();

8

try {

9

while (e.MoveNext()) {

10

V v = (V)(T)e.Current;

11

embedded-statement

12

}

13

}

14

finally {

15

… // Dispose e

16}

17}

18The variable e is not visible to or accessible to the expression x or the embedded statement or any other

19souce code of the program. The variable v is read-only in the embedded statement. If there is not an explicit

20conversion (§13.2) from T (the element type) to V (the type in the foreach statement), an error is produced

21and no further steps are taken. [Note: If x has the value null, a System.NullReferenceException is

22thrown at run-time. end note]

23The body of the finally block is constructed according to the following steps:

24First determine whether the enumerator type E has an appropriate Dispose method:

25o Perform member lookup on the type E with identifier Dispose and no type arguments. If the

26member lookup does not produce a match or produces an ambiguity or produces a match that is not

27a method group, check for the System.IDisposable interface as described below. It is

28recommended that a warning be issued if member lookup produces anything except a method group

29or no match.

30o Perform overload resolution using the resulting method group and an empty argument list. If

31overload resolution results in no applicable methods or results in an ambiguity or results in a single

32best method but that method is either static or not public or has a return type other than void, check

33for the System.IDisposable interface as described below. It is recommended that a warning be

34issued if overload resolution produces anything except an unambiguous public instance method with

35void return type or no applicable methods.

36o The finally clause is expanded to:

37finally {

38e.Dispose();

39}

40Otherwise, check for the System.IDisposable interface:

41o If there is an implicit conversion from E to the System.IDisposable interface, then the finally

42clause is expanded to:

43finally {

44((System.IDisposable)e).Dispose();

45}

46o Otherwise, if E is a sealed type, the finally clause is expanded to an empty block:

47finally {

48}

49o Otherwise, the finally clause is expanded to:

228

Соседние файлы в предмете Электротехника