English 中文(简体)
c# switch statement question
原标题:

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.

最佳回答

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.

问题回答

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)

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

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.

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.

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签