C# (Csharp) Switch Case Statement with Example

A C# switch case statement defines multiple groups of statements, and then executes one of these statement; depending on the value of a constant or test expression.

Let us understand this concept with an example.

string siteName = "DevCurry";
switch (siteName) {
    case "DotNetCurry":
    Console.WriteLine("This website is DotNetCurry.com");
    break;

    case "DevCurry":
    Console.WriteLine("This website is DevCurry.com");
    break;

    default:
    Console.WriteLine("Default Website");
}

As you can see, we start by defining a constant at the top:

string siteName = "DevCurry";

The constant or expression siteName is evaluated at the top of the switch construct. The expression can be an integer, a string, a char, or an enum.

The switch statement can contain zero or more switch sections. In our case, a switch section looks like the following:

case "DotNetCurry":
Console.WriteLine("This website is DotNetCurry.com");
break;

Each of these switch section starts with one or more case labels. In our case, a case label looks like the following:

case "DotNetCurry":
case "DevCurry":

The constant value siteName is compared with the case labels. If the value of siteName matches the constant, the respective switch statement is executed.

At the end of each case, you must explicitly specify how does the switch section end. This is done using a jump statement. You can use one of the following jump statements:

  • break (goes to the end of the switch)
  • goto case “case label” (goes to a different case)
  • goto default (jumps to the default case)
  • You can also use return, throw or continue

In our example, we have used break. Since the expression siteName contains “DevCurry” and that matches the second switch section, the statements in the second switch statement gets executed, and you see the message “This website is DevCurry.com”. The program encounters break and jumps to the end of the switch statement.

The default section, which is an optional section, will run if none of the switch sections match the expression.

Handling Common Cases in Switch Case


There can be a possibility that more than one constant executes the same code. In this case, you can list the common cases sequentially:

switch (oddNumber)
{
    case 1:
    case 3:
    case 5:
        Console.WriteLine ("This is an odd number");
        break;

    default:
        Console.WriteLine ("Default case encountered");
        break;
}

Over here constant 1, 3 and 5 are odd numbers and hence we have defined them sequentially against one common case.

Switch Case Statement inside For Loop


You can even have a switch case statement inside a for loop as follows:

for( int a=1; a<4; a++ )
{
    switch( a )
    {
        case 4:
        Console.WriteLine("a is {0}", a);
        break;
    }
}

As you may have guessed, the output will be a is 4.

Switch case statements can be very useful in replacing multiple if-else statements.

Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: