2

I am trying to make an intelligent switch statement instead of using 20+ if statements. I tried this

private int num;
switch(num)
{
    case 1-10:
        Return "number is 1 through 10"
        break;
    default:
        Return "number is not 1 through 10"
}

It says cases cannot fall through each other.

Thanks for any help!

Shar1er80
  • 8,921
  • 2
  • 18
  • 27
nathan
  • 377
  • 1
  • 2
  • 9
  • 3
    Why not use a single if/else block with a condition that looks something like `if (num >= 1 && num <= 10)`? – adv12 Jul 27 '15 at 19:24
  • 1
    Related if not duplicate: http://stackoverflow.com/a/13927939/961113 – Habib Jul 27 '15 at 19:28
  • I would suggest using the tools that are in front of you for example `Google C# switch case statement syntax` also what is the method signature of the code belongs in Retun is a key word and is lower case – MethodMan Jul 27 '15 at 19:28

4 Answers4

15

With recent changes introduced in C# 7, it is now possible to switch on a range.

Example:

int i = 63;

switch (i)
{
    case int n when (n >= 10):
    Console.WriteLine($"I am 10 or above: {n}");
    break;

    case int n when (n < 10 && n >= 5 ):
    Console.WriteLine($"I am between 10 and 5: {n}");
    break;

    case int n when (n < 5):
    Console.WriteLine($"I am less than 5: {n}");
    break;
}

Note: This really doesn't help the OP much, but hopefully it will help someone looking for this in the future.

Steve Gomez
  • 8,746
  • 7
  • 37
  • 44
6

Your syntax for trying to do a range with switch/case is wrong.

case 1 - 10: will be translated to case -9:

There are two ways you can attempt to cover ranges (multiple values):

List the cases individually

case 1: case 2: case 3: case 4: case 5:
case 6: case 7: case 8: case 9: case 10:
    return "Number is 1 through 10";
default:
    return "Number is not 1 though 10";

Calculate a range

int range = (number - 1) / 10;
switch (range)
{
    case 0: // 1 - 10
        return "Number is 1 through 10";
    default:
        return "Number is not 1 though 10";
}

HOWEVER

You really should consider covering ranges of values with an if statement

if (1 <= number && number <= 10)
    return "Number is 1 through 10";
else
    return "Number is not 1 through 10";
Shar1er80
  • 8,921
  • 2
  • 18
  • 27
1

No, there's no syntax for a "range" within a switch case. If you don't want to list individual cases than an if/else will be cleaner:

if(num >= 1 && num <= 10)
    Return "number is 1 through 10";
else    
    Return "number is not 1 through 10";

which can also be shortened with the conditional operator:

return (num >= 1 && num <= 10)
    ? "number is 1 through 10"
    : "number is not 1 through 10";

I would use whichever is easiest to read and understand by someone who didn't write it.

D Stanley
  • 144,385
  • 11
  • 166
  • 231
1

I know I'm very late to the party, but in case anyone is wondering how this is done now, then have a look at this example:

public string IsBetween1And10(int num)
{
    return num switch
    {       
        >= 1 and <= 10 => "number is 1 through 10",
        _ => "number is not 1 through 10"
    };
}
uzilan
  • 2,364
  • 1
  • 28
  • 45