[Mono-list] Helo in switch-case:

John Barnette jbarn@httcb.net
Sat, 16 Mar 2002 01:25:26 -0700


At 01:27 AM 3/16/2002, Gaurav Vaish wrote:
>----- Original Message -----
>From: "John Barnette" <jbarn@httcb.net>
>To: "Gaurav Vaish" <gvaish@iitk.ac.in>; <mono-list@ximian.com>
>Sent: Saturday, March 16, 2002 12:59
>Subject: Re: [Mono-list] Helo in switch-case:
>
>
>: Gaurav,
>:
>: Even default: labels must have a break in C#, I believe.
>
>     On the contrast, my "break" statements are redundant. C# does not 
> believe in
>"break" - it does it by default. This is in exactly opposite direction 
>with what
>we have in other languages (C/C++/Java) where the flow is automatically
>transferred to next label if no break if found.
>
>     In C#, whether or not break is there, the control is transferred to 
> the end
>of the block. So, applying break to default may not help.
>
>     Hey wait.. I just tried out, it's surprising (and also disgusting) to see
>this. Applying "break" worked.

Gaurav,

 From the ECMA C# spec, 15.7.2:

"If the end point of the statement list of a switch section is reachable, a 
compile-time error occurs.  This is known as the "no fall through" 
rule.  [Example: The example

         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 another switch section, an explicit 'goto case' or 'goto 
default' statement must be used."


~ j. // I, for one, am a fan of axing default fall-through.  Along with
      // promoting some really odd code errors, it makes evil things like
      // Duff's Device possible. ;-)