C# Keywords Tutorial Part 82: switch

C# Keywords Tutorial Part 82: switch

In C#, the switch keyword is used to create a control structure that tests a variable against a list of possible values and executes a block of code based on which value the variable matches. In this blog post, we will explore the switch keyword in more detail and provide code examples to help you understand its usage.

Basic Syntax

The basic syntax for a switch statement in C# is as follows:

switch (variable)
{
    case value1:
        // code to execute if variable == value1
        break;
    case value2:
        // code to execute if variable == value2
        break;
    // add more cases as needed
    default:
        // code to execute if variable doesn't match any case
        break;
}

In this example, variable is the variable being tested against the list of possible values. The case statements list the possible values that variable can match, and the code inside each case block is executed if variable matches that value. The default block is executed if variable does not match any of the listed values.

Here’s an example that demonstrates the basic syntax:

int day = 3;
string dayName;

switch (day)
{
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    case 4:
        dayName = "Thursday";
        break;
    case 5:
        dayName = "Friday";
        break;
    case 6:
        dayName = "Saturday";
        break;
    case 7:
        dayName = "Sunday";
        break;
    default:
        dayName = "Unknown";
        break;
}

Console.WriteLine(dayName); // Output: "Wednesday"

In this example, day is set to 3, so the case 3 block is executed, setting dayName to “Wednesday”.

Multiple Cases

You can list multiple cases that should be handled in the same way by separating them with a comma. Here’s an example:

int number = 3;
string numberType;

switch (number)
{
    case 1:
    case 3:
    case 5:
        numberType = "odd";
        break;
    case 2:
    case 4:
    case 6:
        numberType = "even";
        break;
    default:
        numberType = "unknown";
        break;
}

Console.WriteLine(numberType); // Output: "odd"

In this example, number is set to 3, so the case 1, case 3, and case 5 blocks are all executed, setting numberType to “odd”.

Using Patterns

Starting with C# 7.0, you can use patterns in case statements to match against more complex conditions. For example, you can use a type pattern to check the type of an object, or a property pattern to check the value of a property. Here’s an example:

object obj = 3;
string objType;

switch (obj)
{
    case int i:
        objType = "integer";
        break;
    case string s:
        objType = "string";
        break;
    case double d when d > 0:
        objType = "positive double";
        break;
    default:
        objType = "unknown";
        break;
}

Console.WriteLine(objType); // Output: "integer"

In this example, obj is set to an integer value, so the case int i: block is executed, setting objType to “integer”.

The when keyword can be used to add additional conditions to a pattern match. In this example, the case double d when d > 0: block is executed only if the value of obj is a double and the value is greater than zero.

Using the switch Statement with Enumerations

Enumerations in C# provide a way to define a set of named constants. You can use switch statements with enumeration values to make your code more readable and less error-prone. Here’s an example:

enum Color
{
    Red,
    Green,
    Blue
}

Color color = Color.Green;

switch (color)
{
    case Color.Red:
        Console.WriteLine("The color is red.");
        break;
    case Color.Green:
        Console.WriteLine("The color is green.");
        break;
    case Color.Blue:
        Console.WriteLine("The color is blue.");
        break;
    default:
        Console.WriteLine("Unknown color.");
        break;
}

In this example, color is set to Color.Green, so the case Color.Green: block is executed, printing “The color is green.” to the console.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *