X hits on this document

1016 views

0 shares

0 downloads

0 comments

185 / 396

C# LANGUAGE SPECIFICATION

switch (i) { default: CaseAny(); break; case 1: CaseZeroOrOne(); goto default; case 0: CaseZero(); goto case 1; }

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; }

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; } }

Like the string equality operators (§‎7.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.

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

The statement-lists of a switch-block may contain declaration statements (§‎8.5). The scope of a local variable or constant declared in a switch block is the switch block.

Within a switch block, the meaning of a name used in an expression context must always be the same (§‎7.5.2.1).

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:

172Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1016
Page views1016
Page last viewedFri Dec 09 19:32:32 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments