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

C# LANGUAGE SPECIFICATION

1The type of a local-constant-declaration specifies the type of the constants introduced by the declaration.

2The type is followed by a list of constant-declarators, each of which introduces a new constant. A constant-

3declarator consists of an identifier that names the constant, followed by an “=” token, followed by a

4constant-expression (§14.15) that gives the value of the constant.

5The type and constant-expression of a local constant declaration shall follow the same rules as those of a

6constant member declaration (§17.3).

7The value of a local constant is obtained in an expression using a simple-name (§14.5.2).

8The scope of a local constant is the block in which the declaration occurs. It is an error to refer to a local

9constant in a textual position that precedes its constant-declarator. Within the scope of a local constant, it is

10a compile-time error to declare another local variable or constant with the same name.

11A local constant declaration that declares multiple constants is equivalent to multiple declarations of single

12constants with the same type.

1315.6 Expression statements

14An expression-statement evaluates a given expression. The value computed by the expression, if any, is

15discarded.

16expression-statement:

17

statement-expression ;

18

statement-expression:

19

invocation-expression

20

object-creation-expression

21

assignment

22

post-increment-expression

23

post-decrement-expression

24

pre-increment-expression

25

pre-decrement-expression

26Not all expressions are permitted as statements. [Note: In particular, expressions such as x + y and

27x == 1, that merely compute a value (which will be discarded), are not permitted as statements. end note]

28Execution of an expression statement evaluates the contained expression and then transfers control to the

29end point of the expression statement. The end point of an expression-statement is reachable if that

30expression-statement is reachable.

3115.7 Selection statements

32Selection statements select one of a number of possible statements for execution based on the value of some

33expression.

34selection-statement:

35

36

if-statement switch-statement

3715.7.1 The if statement

38The if statement selects a statement for execution based on the value of a boolean expression.

39if-statement:

40

if

(

boolean-expression ) embedded-statement

41

if

(

boolean-expression ) embedded-statement else embedded-statement

42

boolean-expression:

43

expression

44An else part is associated with the lexically nearest preceding if that is allowed by the syntax. [Example:

45Thus, an if statement of the form

220

Chapter 15 Statements

1if (x) if (y) F(); else G();

2is equivalent to

3if (x) {

4

if (y) {

5

F();

6

}

7

else {

8

G();

9}

10}

11end example]

12An if statement is executed as follows:

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

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

15if control reaches the end point of that statement, control is transferred to the end point of the if

16statement.

17If the boolean expression yields false and if an else part is present, control is transferred to the

18second embedded statement. When and if control reaches the end point of that statement, control is

19transferred to the end point of the if statement.

20If the boolean expression yields false and if an else part is not present, control is transferred to the

21end point of the if statement.

22The first embedded statement of an if statement is reachable if the if statement is reachable and the

23boolean expression does not have the constant value false.

24The second embedded statement of an if statement, if present, is reachable if the if statement is reachable

25and the boolean expression does not have the constant value true.

26The end point of an if statement is reachable if the end point of at least one of its embedded statements is

27reachable. In addition, the end point of an if statement with no else part is reachable if the if statement is

28reachable and the boolean expression does not have the constant value true.

2915.7.2 The switch statement

30The switch statement selects for execution a statement list having an associated switch label that corresponds

31to the value of the switch expression.

32switch-statement:

33

switch ( expression ) switch-block

34 switch-block:

35{ switch-sectionsopt }

36switch-sections:

37

switch-section

38

switch-sections switch-section

39

switch-section:

40

switch-labels statement-list

41

switch-labels:

42

switch-label

43

switch-labels switch-label

44

switch-label:

45

case constant-expression :

46

default :

221

C# LANGUAGE SPECIFICATION

1A switch-statement consists of the keyword switch, followed by a parenthesized expression (called the

2switch expression), followed by a switch-block. The switch-block consists of zero or more switch-sections,

3enclosed in braces. Each switch-section consists of one or more switch-labels followed by a statement-list

4(§15.2.1).

5The governing type of a switch statement is established by the switch expression. If the type of the switch

6expression is sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type,

7then that is the governing type of the switch statement. Otherwise, exactly one user-defined implicit

8conversion operator (§13.4) shall exist from the type of the switch expression or a base type of this type to

9one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong,

10char, string. If no such implicit conversion operator exists, or if more than one such implicit conversion

11operator exists, a compile-time error occurs.

12The constant expression of each case label shall denote a value of a type that is implicitly convertible

13(§13.1) to the governing type of the switch statement. A compile-time error occurs if two or more case

14labels in the same switch statement specify the same constant value.

15There can be at most one default label in a switch statement.

16A switch statement is executed as follows:

17The switch expression is evaluated and converted to the governing type.

18If one of the constants specified in a case label in the same switch statement is equal to the value of

19the switch expression, control is transferred to the statement list following the matched case label.

20If none of the constants specified in case labels in the same switch statement is equal to the value of

21the switch expression, and if a default label is present, control is transferred to the statement list

22following the default label.

23If none of the constants specified in case labels in the same switch statement is equal to the value of

24the switch expression, and if no default label is present, control is transferred to the end point of the

25switch statement.

26If the end point of the statement list of a switch section is reachable, a compile-time error occurs. This is

27known as the “no fall through” rule. [Example: The example

28switch (i) {

29case 0:

30 CaseZero();

31break;

32case 1:

33 CaseOne();

34break;

35default:

36 CaseOthers();

37break;

38}

39is valid because no switch section has a reachable end point. Unlike C and C++, execution of a switch

40section is not permitted to “fall through” to the next switch section, and the example

41switch (i) {

42case 0:

43CaseZero();

44case 1:

45CaseZeroOrOne();

46default:

47CaseAny();

48}

49results in a compile-time error. When execution of a switch section is to be followed by execution of another

50switch section, an explicit goto case or goto default statement shall be used:

222

Chapter 15 Statements

1switch (i) {

2case 0:

3 CaseZero();

4goto case 1;

5case 1:

6 CaseZeroOrOne();

7goto default;

8default:

9 CaseAny();

10break;

11}

12end example]

13Multiple labels are permitted in a switch-section. [Example: The example

14switch (i) {

15case 0:

16 CaseZero();

17break;

18case 1:

19 CaseOne();

20break;

21case 2:

22default:

23 CaseTwo();

24break;

25}

26is valid. The example does not violate the “no fall through” rule because the labels case 2: and default:

27are part of the same switch-section. end example]

28[Note: The “no fall through” rule prevents a common class of bugs that occur in C and C++ when break

29statements are accidentally omitted. In addition, because of this rule, the switch sections of a switch

30statement can be arbitrarily rearranged without affecting the behavior of the statement. For example, the

31sections of the switch statement above can be reversed without affecting the behavior of the statement:

32switch (i) {

33default:

34 CaseAny();

35break;

36case 1:

37 CaseZeroOrOne();

38goto default;

39case 0:

40 CaseZero();

41goto case 1;

42}

43end note]

44[Note: The statement list of a switch section typically ends in a break, goto case, or goto default

45statement, but any construct that renders the end point of the statement list unreachable is permitted. For

46example, a while statement controlled by the boolean expression true is known to never reach its end

47point. Likewise, a throw or return statement always transfers control elsewhere and never reaches its end

48point. Thus, the following example is valid:

49switch (i) {

50case 0:

51while (true) F();

52case 1:

53throw new ArgumentException();

54case 2:

55return;

56}

57end note]

58[Example: The governing type of a switch statement can be the type string. For example:

223

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