Multiple Cases of Switch Expression for Same Result in C#

Welcome to mycodebit.com. In this article, we will discuss Multiple Cases of Switch Expression for same result in C# that have the same results. We will combine cases in switch statements in relational patterns and constant patterns. In the relational pattern we will compare expression results to the constant and with the constant pattern, we will test if the constant is equal to the expression result.

If you want to increase your productivity as a .NET developer check this post.

Click Me

I have listed some examples to understand techniques to combine case statements.

Ordinary Switch Statement to get Same Results:

Multiple cases with the same results in an ordinary switch statement by omitting the break to get the same results.

public static void MultipleCaseResults(int input)
        {
            string output;
            switch (input)
            {
                case 30:
                case 40:
                case 50:
                    output = "How may I help you?";
                    break;
                case 60:
                    output = "Oh my gosh!!";
                    break;
                default:
                    output = "Is annoying by the way.";
                    break;
            }
            Console.WriteLine(output);
        }

If the input will be (30,40,50) the output/result will be the same “How may I help you?”.

Using the when Keyword with Relational and Logical Operators

Use of “when” Keyword with Relational and Logical Operators:

By using the “when” keyword we can apply a condition into the case block while creating switch expressions with multiple cases.

public static void UsewhenMultipleCases(int input)
        {
            switch (input)
            {
                case int x when (x >= 200 && x <= 300):
                    Console.WriteLine("The value is between 200 and 300");
                    break;
                case int n when (n >= 400 && n <= 500):
                    Console.WriteLine("The value is between 400 and 500");
                    break;
                default:
                    Console.WriteLine("The number is not within the specific range.");
                    break;
            }
        }

In this example, we are using when keyword into the case block to get the same results for the multiple inputs without using multiple case blocks. As you can see we will get the same result output “The value is between 200 and 300” if the input will be any between 200 to 300.

Contains Method in Switch Statement:

We can use contains method to check if the prior defined array has the specific value to return the same result.

public static void MultipleCaseWithListValues(int input)
        {
            var list = new List<int> { 30, 40, 50 };

            var output = input switch
            {
                var x when list.Contains(x) => "How may I help you?",
                60 => "Oh my gosh!!",
                70 => "Is annoying by the way.",
                _ => "No way",
            };

            Console.WriteLine($"{output} - result when using a specific list");
        }

Now let’s see a similar example using Pattern matching improvement in C# 9.0.

public static void MultipleCasePatternmatching(int input)
        {
            var output = input switch
            {
                30 or 40 or 50 => "How may I help you?",
                60 => "Oh my gosh!!",
                70 => "Is annoying by the way.",
                > 80 => "No way",
                _ => "Some dummy text.",
            };

            Console.WriteLine($"{output} pattern matching in C# 9.0");

I’m using here disjunctive “or” pattern. we can also use the conjunctive “and” pattern as well if required.

Conclusion:

In this article, we have learned some different techniques if we want to return the same output for multiple cases in a switch statement. If you have any suggestions to make our post better we are open to accepting your comments and you can also contact us.

Leave a Reply

Your email address will not be published.