C# switch: case not falling through to other cases limitation

Posted by Mike Fielden on Stack Overflow See other posts from Stack Overflow or by Mike Fielden
Published on 2008-09-09T16:36:18Z Indexed on 2010/04/01 23:53 UTC
Read the original article Hit count: 367

Filed under:
|

This question is kind of an add-on to this question

In C#, a switch case cannot fall through to other cases, this causes a compilation error. In this case I am just adding some number to the month total for the selected month and each subsequent month thereafter. (simple example, not meant to be real)

switch (month)
{
    case 0:
      add something to month totals
    case 1:
      add something to month totals
    case 2:
      add something to month totals
    default:
      break;
}

Is there a logical alternative to this in C# without having to write out a ton of if statements?

if (month <= 0)
   add something to month
if (month <= 1)
   add something to month
if (month <= 2)
   add something to month
.... etc

© Stack Overflow or respective owner

Related posts about c#

Related posts about switch-statement