159

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.

a3f
  • 8,517
  • 1
  • 41
  • 46
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248

7 Answers7

394

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

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

    case 7:
    case 12:
    ...
    break;
    ...
}
Jose Basilio
  • 50,714
  • 13
  • 121
  • 117
  • 6
    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). – paxdiablo May 11 '09 at 14:54
  • This was exactly what I was looking for. Good job, your work is appreciated. – Chris Apr 27 '17 at 13:22
43

You do it by stacking case labels:

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

    case 7: 
    case 12:
    ...
    break;
    ...
}
David Webb
  • 190,537
  • 57
  • 313
  • 299
26

You may do this as of C# 9.0:

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

    case 7 or 12:
        // ...
        break;
    // ...
}
Kot
  • 445
  • 4
  • 9
  • This is more readable then allowing it fall through, downside it's C# 9.0 up only as you pointed out. – henda79 Nov 09 '21 at 12:27
22
case 2:
case 5:
do something
break;
On Freund
  • 4,376
  • 2
  • 23
  • 30
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... }

AnnaR
  • 3,166
  • 6
  • 35
  • 39
  • 5
    Note that this is only true for empty cases. Cases with actual body do not automatically fall through. – On Freund May 11 '09 at 14:58
6

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.

SymboLinker
  • 884
  • 6
  • 15
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.");
   }
}
IAmJersh
  • 742
  • 8
  • 25
gimel
  • 83,368
  • 10
  • 76
  • 104