Switch case can I use a range instead of a one number

0 votes

I'd like to utilise switch, but I have a lot of cases; is there a way to save time? So far, the only remedy I've come across that I've attempted is:

switch (number)
{
case 1: something; break;
case 2: other thing; break;
...
case 9: .........; break;
}

I'm aiming to be able to do something along the lines of:

switch (number)
{
case (1 to 4): do the same for all of them; break;
case (5 to 9): again, same thing for these numbers; break;
}
Jun 11 in C# by pranav
• 2,320 points
8 views

1 answer to this question.

0 votes

To categorise your switch circumstances, I would use ternary operators.

switch( number > 9 ? "High" :
        number > 5 ? "Mid" :
        number > 1 ? "Low" : "Floor")
        {
              case "High":
                    do the thing;
                    break;
               case "Mid":
                    do the other thing;
                    break;
               case "Low":
                    do something else;
                    break;
               case "Floor":
                    do whatever;
                    break;
         }
answered Jun 13 by krishna
• 1,340 points

Related Questions In C#

0 votes
1 answer

C# - Can someone please show me a very simple example of Interfaces

Take this as example interface IFlyable { ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
14 views
0 votes
1 answer

How do I specify the exit code of a console application in .NET

Simply put, you can return the exit ...READ MORE

answered Jun 11 in C# by pranav
• 2,320 points
20 views
0 votes
1 answer

Why does one use dependency injection?

I believe that many people are perplexed ...READ MORE

answered Jun 6 in C# by pranav
• 2,320 points
16 views
0 votes
1 answer

Which version of C# am I using

From developer cmd, type this csc -langversion:? This will ...READ MORE

answered Jun 7 in C# by pranav
• 2,320 points
11 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,100 points
4,468 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
499 views
+6 votes
16 answers

How do backend of these really cool games work?

Most of the games these days don't ...READ MORE

answered Jul 19, 2018 in Career Counselling by Kalgi
• 52,350 points
7,253 views
0 votes
1 answer
0 votes
1 answer

How do I turn a C# object into a JSON string in .NET

Newtonsoft.json can be used to accomplish this. ...READ MORE

answered Jun 13 in C# by krishna
• 1,340 points
9 views
0 votes
1 answer

How to initialize a list of strings (List<string>) with many string values

Simply remove the () at the end. List<string> ...READ MORE

answered Jun 13 in C# by krishna
• 1,340 points
10 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP