tips on switch case — no fall through in C#

Here’s a list of the C#-specific rules for using the switch statement:
A switch block consists of zero or more cases.
The switch expression can be of these types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or enum.
There can only be one default label, and it is optional. If none of the case statements match the switch value, execution is transferred to the default label.
Execution of the switch section is not permitted to “fall through” as is the case in C, C++, and Java. This means that you must code the break statement at the end of each case evaluation.
Since execution is not permitted to fall through, you can put the case statements in any order.
A case selection can have multiple labels as in the following:case 12 :
case 42 :
default :
do something here;
break;
Note
Java and C++ programmers—look out for the no “fall through” rule. You will be tempted to use it, and C# will not allow it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s