How does control flow from one switch chassis to another without interrupting C #?

Just started C#

and I learned that you cannot go from one case switch

to the next if there is even one statement. Error: Control cannot fall through from one case to another

.

Just for fun stuff, I tried this:

        char c = 'a';
        switch (c)
        {
            case 'a':
                Console.WriteLine("yes");
                goto JumpToNextCase;
            case 'b':
                 JumpToNextCase:
                Console.WriteLine("Kiddin me?");
                break;

        }

      

And it worked! How can it be? Am I now not breaking the rule to jump from one case to another? Any satisfactory answer?

+3


source to share


4 answers


Yes, the limitation is only to prevent what is 99% of the time an accident and error.

However, you don't even need a shortcut to make it work if you think you want it to work. You can use goto

to navigate to different case

!

char c = 'a';
switch (c)
{
    case 'a':
        Console.WriteLine("yes");
        goto case 'b';
    case 'b':
        Console.WriteLine("Kiddin me?");
        break;
}

      



You can also go to default

. The C # spec indicates that:

8.9.3 The goto statement The goto statement transfers control to the statement marked with a label.

goto-statement:
goto   identifier   ;
goto   case   constant-expression   ;
goto   default   ;

      

(...)

The target of a statement goto case

is a list of statements in the immediate conclusion of a switch statement (§8.7.2) that contains a label case

with a specified constant value.

(...)

The target of a statement goto default

is a list of statements in the immediate conclusion of a switch statement (§8.7.2) that contains a label default

.

+7


source


Falling through cases is a safety net, not some fundamental limitation for the language. Previous languages ​​like C allow you to fail without warning or error, but usually it's a bug and you've just forgotten break

. C # introduced a bug to prevent these bugs.



Nothing stops you from getting around it, just like you. However, you had to do it on purpose and not accidentally, and therefore (hopefully) won't introduce errors while doing this.

+8


source


It worked because you were using the operator goto

.

goto

redirects the flow of the program, ending this case. You can also use return

(instead of break

) and it will compile just fine.

The compiler only complains when the case doesn't end up somehow (fails).

I'm sure you know that, but goto

it's never a good idea anyway :)

+2


source


A crash is not allowed mainly because the programmer can easily forget break;

, and this is likely to be a mistake because they never planned to run the second block case

. In this case, you are explicitly asking for another block to be executed, so this is clearly not something you did by accident. C # just forces you to be more persistent to ask it to run another case to ensure that the failure doesn't happen by accident.

+2


source







All Articles