Switch on string value - CSharp Language Basics

CSharp examples for Language Basics:switch

Description

Switch on string value

Demo Code

class names/*from  w w  w. ja  v a 2s. c o m*/
{
    public static void Main()
    {
        string name = "K";
        System.Console.WriteLine("Starting the switch... {0}", name);
        switch (name)
        {
            case "R":
                System.Console.WriteLine("Hi R!");
                break;
            case "A":
                System.Console.WriteLine("Hi R!");
                break;
            case "B":
                System.Console.WriteLine("Hi B!");
                break;
            case "K":
                System.Console.WriteLine("Yo!");
                break;
            default:
                System.Console.WriteLine("Hi {0}", name);
                break;
        }
        System.Console.WriteLine("The switch statement is now over!");
    }
}

Result


Related Tutorials