switch (i) { case 0: CaseZero(); break; case 1: CaseOne(); break; default: CaseOthers(); break; }is valid because no switch section has a reachable end point. Unlike C and C++, execution of a switch section is not permitted to "fall through" to the next switch section, and the example
switch (i) { case 0: CaseZero(); case 1: CaseZeroOrOne(); default: CaseAny(); }results in a compile-time error. When execution of a switch section is to be followed by execution of another switch section, an explicit goto case or goto default statement must be used:
switch (i) { case 0: CaseZero(); goto case 1; case 1: CaseZeroOrOne(); goto default; default: CaseAny(); break; }end example] Paragraph 81 Multiple labels are permitted in a switch-section. [Example: The example
switch (i) { case 0: CaseZero(); break; case 1: CaseOne(); break; case 2: default: CaseTwo(); break; }is valid. The example does not violate the "no fall through" rule because the labels case 2: and default: are part of the same switch-section. end example] [Note: The "no fall through" rule prevents a common class of bugs that occur in C and C++ when break statements are accidentally omitted. In addition, because of this rule, the switch sections of a switch statement can be arbitrarily rearranged without affecting the behavior of the statement. For example, the sections of the switch statement above can be reversed without affecting the behavior of the statement:
switch (i) { default: CaseAny(); break; case 1: CaseZeroOrOne(); goto default; case 0: CaseZero(); goto case 1; }end note] [Note: The statement list of a switch section typically ends in a break, goto case, or goto default statement, but any construct that renders the end point of the statement list unreachable is permitted. For example, a while statement controlled by the boolean expression true is known to never reach its end point. Likewise, a throw or return statement always transfers control elsewhere and never reaches its end point. Thus, the following example is valid:
switch (i) { case 0: while (true) F(); case 1: throw new ArgumentException(); case 2: return; }end note] [Example: The governing type of a switch statement may be the type string. For example:
void DoCommand(string command) { switch (command.ToLower()) { case "run": DoRun(); break; case "save": DoSave(); break; case "quit": DoQuit(); break; default: InvalidCommand(command); break; } }end example] [Note: Like the string equality operators (§14.9.7), the switch statement is case sensitive and will execute a given switch section only if the switch expression string exactly matches a case label constant. end note] Paragraph 91 When the governing type of a switch statement is string, the value null is permitted as a case label constant. Paragraph 101 The statement-lists of a switch-block may contain declaration statements (§15.5). 2 The scope of a local variable or constant declared in a switch block is the switch block. Paragraph 111 Within a switch block, the meaning of a name used in an expression context must always be the same (§14.5.2.1). Paragraph 121 The statement list of a given switch section is reachable if the switch statement is reachable and at least one of the following is true:
| |
Jagger Software Ltd | |
Company # 4070126 | |
VAT # 762 5213 42 |