<p>This question is kind of an add-on to this <a href="https://stackoverflow.com/questions/44905/c-switch-statement-limitations-why">question</a></p>

<p>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) </p>

<pre><code>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;
}
</code></pre>

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

<pre><code>if (month &lt;= 0)
   add something to month
if (month &lt;= 1)
   add something to month
if (month &lt;= 2)
   add something to month
.... etc
</code></pre>

[{'answer_id': 52314, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 1, 'selected': False, 'text': '<p>There is already a question addressing this topic:</p>\n\n<p><a href="https://stackoverflow.com/questions/44905/c-switch-statement-limitations-why">C# switch statement limitations - why?</a></p>\n\n<p>EDIT:</p>\n\n<p>My main purpose in pointing that out, gentlebeasts, is that two questions of near-identical name add confusion to the pool of questions. </p>\n'}, {'answer_id': 52318, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 5, 'selected': True, 'text': "<p>Often times when you see the noise from a huge switch statement or many if statements that might fall into more than one block, you're trying to suppress a bad design.</p>\n\n<p>Instead, what if you implemented the Specification pattern to see if something matched, and then act on it?</p>\n\n<pre><code>foreach(MonthSpecification spec in this.MonthSpecifications)\n{\n   if(spec.IsSatisfiedBy(month))\n       spec.Perform(month);\n}\n</code></pre>\n\n<p>then you can just add up different specs that match what you're trying to do.</p>\n\n<p>It's hard to tell what your domain is, so my example might be a little contrived.</p>\n"}, {'answer_id': 52323, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 1, 'selected': False, 'text': '<p>Are you adding constants?  If so, maybe something like this would work(C syntax):</p>\n\n<pre><code>const int addToTotals[] = {123, 456, ..., 789};\n\nfor(i=month;i&lt;12;i++)\n   totals += addToTotals[i];\n</code></pre>\n\n<p>You can do a similar thing with variable or function pointers if you need more complex statements than add constant to totals for each month following.</p>\n\n<p>-Adam</p>\n'}, {'answer_id': 52388, 'author': 'jwarzech', 'author_id': 3111, 'author_profile': 'https://Stackoverflow.com/users/3111', 'pm_score': 3, 'selected': False, 'text': '<p>In C# switch statements you can fall through cases only if there is no statement for the case you want to fall through</p>\n\n<pre><code>switch(myVar)\n{\n   case 1:\n   case 2: // Case 1 or 2 get here\n      break;\n}\n</code></pre>\n\n<p>However if you want to fall through with a statement you must use the dreaded GOTO</p>\n\n<pre><code>switch(myVar)\n    {\n       case 1: // Case 1 statement\n               goto case 2;\n       case 2: // Case 1 or 2 get here\n          break;\n    }\n</code></pre>\n'}, {'answer_id': 2564325, 'author': 'svv', 'author_id': 307360, 'author_profile': 'https://Stackoverflow.com/users/307360', 'pm_score': 0, 'selected': False, 'text': '<p>Write the switch cases in reverse order</p>\n\n<pre><code>case 2:\n\ncase 1:\n\ncase 0:\n\nbreak;\n\n\ndefault:\n</code></pre>\n\n<p>Hope that helps!</p>\n'}]
['https://Stackoverflow.com/questions/52313', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4144/']