views:

104

answers:

6

Hi, I'll cut to the chase. I have two questions about switch that are simple, but I can't figure them out.

First:

in c# switch statements, do case statements have to be consecutive (with ints)?

For example:

switch(someInt)
{
    case 1
    // some code
    case 2
    // some code 
    case 3 
    // some code
}

or is it possible to do something like the following:

switch(someInt)
{
    case 1 
    case 3
    case 5
}

I know that normally if-else statements are used for something like that, but I'm just curious to know if its possible.

Also, is it considered magic numbers to use actual numbers in case statements? Or is is better practice to declare constants for use in the case statements?

Thanks!

Edit:

Thanks to all of you for your responses! I appreciate it.

A: 

They don't have to be consecutive. Although I do that just for clarity's sake.

Scott
+1  A: 

They can be in any order you want. And no, it's not always bad to use actual numbers. But not magic numbers. Use numbers if you are comparing an int, like maybe

switch (numberOfItems) 
{  
    case 0:
      break;
    case 1:
      break;
    default:
      break;
}

(Of course, this is only an example and I can't imagine seeing this code in the real world)

marcc
A: 

Order does not matter, the compiler will do that work for you.

I prefer to use either an enumeration or a const int to provide meaning to the number, particularly when it is being maintained by somebody else down the road.

Chris Patterson
Depends on the purpose of the number in question. For instance, I've switched on counts before. Do I really need an enumeration here?
Matthew Scharley
A: 

The values of the case statements definitely do not need to be consecutive.

You also aren't tied to only using integer values. Strings work just as well.

If you're worried about magic numbers, your best bet is to create an enumeration. It will convey the meaning of those magic numbers. Otherwise, have at it and enjoy.

Justin Niessner
A: 

It's possible to do both. The syntax is this: (you're close)

switch(someInt)
{
    case 1:
    // some code
    break;

    case 2:
    // some code 
    break;

    case 3:
    // some code
    break;

    default:
    // code for "else" case
    break;
}
or is it possible to do something like the following:

switch(someInt)
{
    case 1:
    case 3:
    case 5:
    // some code
    break;
}

Note the colons and breaks.

As for the use of magic numbers, in general, I prefer to put literals in constants, but I make exceptions for glaringly obvious numbers such as the lowest number to check for factor divisibility is 2.

recursive
Got my badge! Thanks for the downmod! :D
JB
A: 

As a small optimization, you can order your case values based on actual/expected frequency. I'd also add a "default" case so you'll be able to easily discover where you've used your enum and forgotten to account for it. This is another reason to use enum values over constants.

No Refunds No Returns
I thought that for ints, all branching was constant time regardless of order. I don't have a source, but does anyone know for sure?
recursive