📜  switch case c# range - C# (1)

📅  最后修改于: 2023-12-03 15:35:13.199000             🧑  作者: Mango

Switch Case Range in C#

In C#, the switch case statement allows us to compare an expression or variable to multiple values and execute different code blocks for each possible match. In C# 7.0 and later versions, the switch statement can also test if an expression falls within a range of values. This is known as switch case range.

Syntax of Switch Case Range

The syntax of the switch case range statement in C# is:

switch (variable)
{
    case value when condition:
        // code block to be executed
        break;
    case value2 when condition2:
        // code block to be executed
        break;
    case var range1 when condition1:
        // code block to be executed
        break;
    case var range2 when condition2:
        // code block to be executed
        break;
    default:
        // code block to be executed if no cases match
        break;
}

In the switch case range statement, the variable is the expression or variable to be compared. The value and value2 are the possible values that the variable can take. The range1 and range2 are the possible ranges of values that the variable can take. The condition, condition1 and condition2 are optional conditions to be checked for the matching values or ranges.

The var keyword allows the declaration of a new variable in each case, which takes the value of the variable and can be used in the code block.

The break statement is used to terminate each code block.

The default case is executed if no other case matches the variable.

Example of Switch Case Range

Let's consider an example to understand switch case range in C#.

Suppose we have a program that takes the age of a person as input and displays a message based on the age range.

int age = 20;

switch (age)
{
    case int n when (n < 0):
        Console.WriteLine("Invalid age");
        break;
    case int n when (n < 18):
        Console.WriteLine("Minor");
        break;
    case int n when (n >= 18 && n <= 65):
        Console.WriteLine("Adult");
        break;
    case int n when (n > 65):
        Console.WriteLine("Senior");
        break;
    default:
        Console.WriteLine("Unknown age");
        break;
}

In this example, we are using switch case range to test if the age of the person falls within a specific range. We are declaring a new variable n for each case, which takes the value of age.

The first case tests if the age is less than 0, which is invalid. The second case tests if the age is less than 18, which means the person is a minor. The third case tests if the age is between 18 and 65, which means the person is an adult. The fourth case tests if the age is greater than 65, which means the person is a senior. The default case is used if the age does not fall within any of the ranges.

Conclusion

Switch case range is a useful feature in C# for comparing an expression or variable to a range of values. It allows programmers to write concise and readable code to handle multiple switch cases.