171

This is what I want to do:

switch(myvar)
{
    case: 2 or 5:
    ...
    break;

    case: 7 or 12:
    ...
    break;
    ...
}

I tried with "case: 2 || 5" ,but it didn't work.

The purpose is to not write same code for different values.

2
  • What do you mean "it didn't work"? Does it give you syntax errors, or logical errors? Commented May 28, 2013 at 20:03
  • Starting with C# 9, this exact syntax is allowed. Commented Jun 23, 2021 at 11:30

7 Answers 7

419

By stacking each switch case, you achieve the OR condition.

switch(myvar)
{
    case 2:
    case 5:
    ...
    break;

    case 7:
    case 12:
    ...
    break;
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Joel, it doesn't support fall through but it DOES support stacking (e.g., an empty case 2 in this answer executes the case 5 section).
This was exactly what I was looking for. Good job, your work is appreciated.
45

You do it by stacking case labels:

switch(myvar)
{
    case 2:
    case 5:
    ...
    break;

    case 7: 
    case 12:
    ...
    break;
    ...
}

Comments

29

You may do this as of C# 9.0:

switch(myvar)
{
    case 2 or 5:
        // ...
        break;

    case 7 or 12:
        // ...
        break;
    // ...
}

1 Comment

This is more readable then allowing it fall through, downside it's C# 9.0 up only as you pointed out.
22
case 2:
case 5:
do something
break;

Comments

18

Case-statements automatically fall through if you don't specify otherwise (by writing break). Therefor you can write

switch(myvar)
{
   case 2:
   case 5:
   {
      //your code
   break;
   }

// etc... }

1 Comment

Note that this is only true for empty cases. Cases with actual body do not automatically fall through.
8

Since C# 8 there are switch expressions that are better readable: no case, : and break;/return needed anymore. Combined with C# 9's logical patterns:

static string GetCalendarSeason(DateTime date) => date.Month switch
{
    3 or 4 or 5 => "spring",
    6 or 7 or 8 => "summer",
    9 or 10 or 11 => "autumn",
    12 or 1 or 2 => "winter",
    _ => throw new ArgumentOutOfRangeException(nameof(date), $"Date with unexpected month: {date.Month}."),
};

Limitation: with this syntax, at the right of the => you cannot use curly braces ({ and }) for statements.

Comments

6

The example for switch statement shows that you can't stack non-empty cases, but should use gotos:

// statements_switch.cs
using System;
class SwitchTest 
{
   public static void Main()  
   {
      Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large"); 
      Console.Write("Please enter your selection: "); 
      string s = Console.ReadLine(); 
      int n = int.Parse(s);
      int cost = 0;
      switch(n)       
      {         
         case 1:   
            cost += 25;
            break;                  
         case 2:            
            cost += 25;
            goto case 1;           
         case 3:            
            cost += 50;
            goto case 1;             
         default:            
            Console.WriteLine("Invalid selection. Please select 1, 2, or3.");            
            break;      
       }
       if (cost != 0)
          Console.WriteLine("Please insert {0} cents.", cost);
       Console.WriteLine("Thank you for your business.");
   }
}

3 Comments

-1 The msdn link has a stacked example further down the page. At any rate, stacked cases work, especially in this question where the stated purpose is to not write duplicate code as done in your case 1 and 2.
Usefull answer as example of "goto case".
I hate goto statements what is this 1992?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.