3

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
  • 9,001
  • 2
  • 20
  • 29
nathan
  • 427
  • 1
  • 3
  • 11
  • 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

5 Answers5

17

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
  • 11,596
  • 7
  • 39
  • 53
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
  • 9,001
  • 2
  • 20
  • 29
2

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"
    };
}
Joséph Flames
  • 204
  • 1
  • 12
uzilan
  • 2,554
  • 2
  • 31
  • 46
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
  • 149,601
  • 11
  • 178
  • 240
0

C#7 and above have added range operators to the switch statement. So now switching based on a range can be done like this:

    var myNumber = 71;
    var message = string.Empty;

    switch (myNumber)
    {
        case var case1 when case1 >= 0 && case1 <= 49:
        {
            message = $"{myNumber} is from 0 to 49.";
            break;
        }
        case var case2 when case2 >= 50 && case2 <= 99:
        {
            message = $"{myNumber} is from 50 to 99.";
            break;
        }
        default
        {
            message = $"{myNumber} is not in an acceptable range.";
            break;
        }
    }
Hoodlum
  • 1,443
  • 1
  • 13
  • 24