13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 15 Statementsswitch (i) {case 0:CaseZero();goto case 1;case 1:CaseZeroOrOne();goto default;default:CaseAny();break;}end example]Multiple labels are permitted in a switch-section. [Example: The exampleswitch (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 breakstatements are accidentally omitted. In addition, because of this rule, the switch sections of a switchstatement can be arbitrarily rearranged without affecting the behavior of the statement. For example, thesections 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 defaultstatement, but any construct that renders the end point of the statement list unreachable is permitted. Forexample, a while statement controlled by the boolean expression true is known to never reach its endpoint. Likewise, a throw or return statement always transfers control elsewhere and never reaches its endpoint. 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:185

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!