C Sharp

The switch Statement

Using the switch statement, you can specify an expression that returns an integral value and one or more pieces of code that will be run depending on the result of the expression. It's similar to using multiple if/else statements, but although you can specify multiple (possibly unrelated) conditional statements with multiple if/else statements, a switch statement consists of only one conditional statement followed by all the results that your code is prepared to handle. Here's the syntax: -

switch (switch_expression)
    {
        case constant-expression:
            statement
            jump-statement
        image
        case constant-expressionN:
            statementN
        [default]
     }

There are two main rules to keep in mind here. First, the switch_expression must be of the type (or implicitly convertible to) sbyte, byte, short, ushort, int, uint, long, ulong, char, or string (or an enum based on one of these types). Second, you must provide a jump-statement for each case statement unless that case statement is the last one in the switch, including the break statement. Because this works differently than in several other languages, I'll explain it more thoroughly later in this chapter in "No Fall-Through in switch Statements." -

Conceptually, the switch statement works just as the if statement does. First, the switch_expression is evaluated, and then the result is compared to each of the constant-expressions or case labels,defined in the different case statements. Once a match is made, control is passed to the first line of code in that case statement.

In addition to letting you specify different case statements, the switch statement also allows for the definition of a default statement. This is like the else clause of an if statement. Note that there can be only one default label for each switch statement and that if no appropriate case label is found for the switch_expression, control is passed to the first line of code after the switch statement's ending brace. Here's an example-a Payment class is using the switch statement to determine which tender has been selected: -

using System;
enum Tenders : int
{
    Cash = 1,
    Visa,
    MasterCard,
    AmericanExpress
};
class Payment
{
    public Payment(Tenders tender)
    {
        this.Tender = tender;
    }
    protected Tenders tender;
    public Tenders Tender
    {
        get
        {
            return this.tender;
        }
        set
        {
            this.tender = value;
        }
    }
    public void ProcessPayment()
    {
        switch ((int)(this.tender))
        {
            case (int)Tenders.Cash:
                Console.WriteLine("\nCash - Accepted");
                break;
            case (int)Tenders.Visa:
                Console.WriteLine("\nVisa - Accepted");
                break;
            case (int)Tenders.MasterCard:
                Console.WriteLine("\nMastercard - Accepted");
                break;
            case (int)Tenders.AmericanExpress:
                Console.WriteLine("\nAmerican Express - Accepted");
                break;
            default:
                Console.WriteLine("\nSorry - Invalid tender");
                break;
        }
    }
}
class SwitchApp
{
    public static void Main()
    {
        Payment payment = new Payment(Tenders.Visa);
        payment.ProcessPayment();
    }
}

Running this application results in the following output because we instantiated the Payment class by passing it a value of Tenders.Visa: -

Visa - Accepted.

by BrainBellupdated
Advertisement: