Switch Case Fall-Through Rules in .NET

Because you cannot accidentally fall through from one case label to the next if there is any intervening code, you can freely rearrange the sections of a switch statement without affecting its meaning (including the default label, which by convention is usually placed as the last label but does not have to be).

C and C++ programmers should note that the break statement is mandatory for every case in a switch statement (even the default case)  This requirement is a good thing; it is common in C or C++ programs to forget the break statement, allowing execution to fall through to the next label and leading to bugs that are difficult to spot.

If you really want to, you can mimic C/C++ fall-through in C# by using a goto statement to go to the following case or default label. Using goto in general is not recommended, though, and this book does not show you how to do it.


Popular posts from this blog

Lambda two tables and three tables inner join code samples

Customize User's Profile in ASP.NET Identity System