Is there a way in .NET to replace code where intervals are compared, for example
if (compare < 10) { // Do one thing } else if (10 <= compare && compare < 20) { // Do another thing } else if (20 <= compare && compare < 30) { // Do yet another thing } else { // Do nothing }
something more elegant like a switch statement (I think in Javascript, "case (<10)" works, but in C #)? Does anyone else find this code ugly?
One simplification: since this is all the rest - if instead of being simple, you do not need to check the negation of the previous conditions. Ie, this is equivalent to your code:
if (compare < 10) { // Do one thing } else if (compare < 20) { // Do another thing } else if (compare < 30) { // Do yet another thing } else { // Do nothing }
, compare >= 10 if, ( ) if s...
compare >= 10
if
, switch C, , if...else if. , .
switch
if...else if
, , - :
switch(compare/10) { case 0: // Do one thing break; case 1: // Do another thing break; case 2: // Do yet another thing break; default; // Do nothing break; }