Linked Questions

163 votes
16 answers
207k views

Switch case: can I use a range instead of a one number [duplicate]

I want to use switch, but I have many cases, is there any shortcut? So far the only solution I know and tried is: switch (number) { case 1: something; break; case 2: other thing; break; ......
user3022162's user avatar
  • 1,659
20 votes
10 answers
21k views

Switch Statement in C# [duplicate]

Does anyone know if it's possible to include a range in a switch statement (and if so, how)? For example: switch (x) { case 1: //do something break; case 2..8: //do something ...
Paul Michaels's user avatar
25 votes
1 answer
24k views

Switch statement with multiple constant-expression in c#. Is it possible? [duplicate]

Possible Duplicate: Multiple Cases in Switch: Is it possible to do a multiple constant-expression switch statement like switch (i) { case "run","notrun", "runfaster": //Something like this. ...
Amra's user avatar
  • 25.4k
12 votes
7 answers
20k views

Switch case, check ranges in C# 3.5 [duplicate]

In C#, the switch statement doesn't allow cases to span ranges of values. I don't like the idea of using if-else loops for this purpose, so are there any other ways to check numeric ranges in C#?
Kenny's user avatar
  • 839
0 votes
0 answers
377 views

Switch case for characters range [duplicate]

How to perform something like this without using a bunch of if-else statements?: char value; switch (value) { case 'a'..'p': do1(); break; case 'q'..'z': do2(); ...
Paul's user avatar
  • 26.8k
-1 votes
3 answers
130 views

Converting if statements to switch but not working, where did conversion go wrong? [duplicate]

This is part of the code, it gets a line of numbers from a textfile and counts how many numbers fit in each specified range. I'm converting it from if, else if statements (which works fine) just for ...
Walter Melon's user avatar
8 votes
6 answers
4k views

How can I use more than one constant for a switch case in C#?

How can I use more than one constant for a switch case C#? Conceptually, I'm looking for something like this: switch(n) { case 1,2,3: //???? case 4: default: }
KentZhou's user avatar
  • 25.7k
25 votes
4 answers
19k views

Using Action dictionaries instead of switch statements

I'm just reviewing some of my old code (have some spare time), and I noticed a rather lengthy switch statement. Due to gaining new knowledge, I have since refactored it in the following form: ...
Darren Young's user avatar
  • 11.1k
12 votes
6 answers
11k views

It is possible to define something like 'between' in if-else statement in C#?

I want to ask someone who has stronger skills than me in C#. Is it possible to reduce the following code if(val > 20 && val < 40 ) ... else if(val > 40 && val < 72 ) ... ...
Snake Eyes's user avatar
  • 16.8k
8 votes
5 answers
23k views

How to use && operator in switch statement based on a combined return a value?

How can I use && operator in switch case? This is what i want to do: private int retValue() { string x, y; switch (x && y) { case "abc" && "1": ...
RachitSharma's user avatar
3 votes
9 answers
4k views

Case/switch statements c#?

I haven't a living clue how to write this in c#. I know how to do it in Delphi. This is how I would do it in Delphi: Case Total of 80..100 : ShowMessage ('You got an A!'); 60..79 : ...
Aidan Quinn's user avatar
  • 1,186
6 votes
3 answers
14k views

How to handle multiple values inside one case? [closed]

How to handle multiple values inside one case? So if I want to execute the same action for value "first option" and "second option"? Is this the right way? switch(text) { case "first option": ...
CodeBreaker's user avatar
5 votes
6 answers
691 views

Incorrect Multiple Cases in Switch not generating compiler error [duplicate]

I know that this code does not work as "expected". Just looking quickly at this code, we think the return value should be 1, but in the execution it returns returns 3. // incorrect variable = 1; ...
Juliano's user avatar
  • 811
-1 votes
2 answers
5k views

Multple Criteria in a Single Case inside a Switch Statement [duplicate]

Hi all I searched and found old answers like in 2008, im creating firefox addon for new browsers so i can use ecma 5+. I was trying to do a a switch that had one block meet multiple criteria like if >...
Noitidart's user avatar
  • 37.6k
0 votes
3 answers
3k views

C# simplify switch statement

Can I somehow simplify this switch statement as both cases do same thing just with another function parameter? switch (data.Subscriber.Protocol) { case "email json": builder....
forrestDumb's user avatar

15 30 50 per page