nathan
nathan

Reputation: 451

C# Switch Between Two Numbers?

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!

Upvotes: 4

Views: 19557

Answers (5)

Hoodlum
Hoodlum

Reputation: 1503

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;
        }
    }

Upvotes: 1

uzilan
uzilan

Reputation: 2624

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"
    };
}

Upvotes: 3

Steve G
Steve G

Reputation: 13397

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.

Upvotes: 17

Shar1er80
Shar1er80

Reputation: 9041

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";

Upvotes: 6

D Stanley
D Stanley

Reputation: 152596

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.

Upvotes: 1

Related Questions