Using switch Statements in C#


Sometimes when we write a cascading if statement, all the if statements look similar because they all evaluate an identical expression. The only difference is that each if compares the result of the expression with a different value. For example, consider the following block of code that uses an if statement to examine the value in the day variable and work out which 
day of the week it is:

if (day == 0)  
    dayName = "Sunday";  
else if (day == 1)  
    dayName = "Monday";  
else if (day == 2)  
    dayName = "Tuesday";  
else if (day == 3)  
    ...  
else  
    dayName = "Unknown";

In these situations, often we can rewrite the cascading if statement as a switch statement to make our program more effcient and more readable.

Syntax:-

The syntax of a switch statement is as follows (switch, case, and default are keywords):

switch ( controllingExpression )  
{  
    case constantExpression :  
        statements  
        break;  
    case constantExpression :  
        statements  
        break;  
    ...  
    default :  
        statements  
        break;  
}

The controllingExpression is evaluated once. Control then jumps to the block of code identifed by the constantExpression, whose value is equal to the result of the controllingExpression. Execution runs as far as the break statement, at which point the switch statement finishes and the program continues at the first statement after the closing brace of the switch statement. If none of the constantExpression values are equal to the value of the controllingExpression, the statements below the optional default label run.

For example, we can rewrite the previous cascading if statement as the following switch statement:

switch (day)  
{  
    case 0 :  
        dayName = "Sunday";  
        break;  
    case 1 :  
        dayName = "Monday";  
        break;  
    case 2 :  
        dayName = "Tuesday";  
        break;  
    ...  
    default :  
        dayName = "Unknown";  
        break;  
}


switch Statement Rules:-

The switch statement is very useful, but unfortunately, we can’t always use it when you might like to. Any switch statement we write must adhere to the following rules:

* We can use switch only on primitive data types, such as int or string. With any other types (including foat and double), you have to use an if statement.

* The case labels must be constant expressions, such as 42 or “42”. If we need to calculate our case label values at run time, we must use an if statement.

* The case labels must be unique expressions. In other words, two case labels cannot have the same value.

* We can specify that we want to run the same statements for more than one value by providing a list of case labels and no intervening statements, in which case the code for the final label in the list is executed for all cases in that list. However, if a label has one or more associated statements, execution cannot fall through to subsequent labels, and the compiler generates an error  For example:

switch (trumps)  
{  
    case Hearts :  
    case Diamonds :      // Fall-through allowed – no code between labels  
        color = "Red";   // Code executed for Hearts and Diamonds  
        break;  
    case Clubs :  
        color = "Black";  
    case Spades :        // Error – code between labels  
        color = "Black";  
        break;  
}

Comments

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples