Question

With the following code:

case "GETSITES":
case "SITESETUP":
    MessageBox.Show("Help! I am suffering from the Air-Conditioned Nightmare!!!");
    // ...

Will the MessageBox.Show be executed either the switch value is "GETSITES" or "SITESETUP"?

Or only if the switch value is "SITESETUP"?

Since "GETSITES" has no break, I'm thinking yes, but am not sure.


UPDATE

I guess the way I should have worded my question as:

Are these two code fragments semantically equivalent?

  • fragment 1

    case 0:
    case 1:
        // bla bla bla;
        break;
    
  • fragment 2(pseudo code)

    case 0, 1:
        // bla bla bla;
        break;
    
Was it helpful?

Solution

What you are describing is having two labels for the same case. C# does allow this. You cannot, however, fall through to the next case statement in the same way that C allows, that is, your label cannot have some code, then fall through to the next case statement. That is forbidden and will cause a compilation error.

OTHER TIPS

Do empty case statements in C# combine with the next non-empty one?

I originally said:

The question supposes a falsehood. There is no such thing as an empty switch section in a legal C# program.

That is not quite true. The C# specification requires that a switch section contain at least one statement, but the implementation actually does not.

I emend my statement as follows:

The question supposes that there is an empty switch section (note that switch sections are not statements), but there can only be an empty switch section in a very unusual situation. First off, let's clearly describe the anatomy of a switch statement.

When you have:

switch(x) 
{ 
   case 1: 
   case 2: 
       Console.WriteLine("Hello");
       break;
}

there is only one switch section. There is no "empty section" following case 1; a section consists of all the case clauses followed by zero or more statements. (The spec requires one or more statements but the implementation actually allows zero statements.) The statement list must have a non-reachable endpoint.

The only way for there to be an empty statement list whose end point is not reachable inside a switch section is for the switch section itself to be unreachable. There are two ways that can happen. Either the entire switch can be unreachable, or the switch can be switching on a constant. In short, the only way to make an empty switch section is either:

switch(1) { case 2: }

or

if (false) switch(x) { case 2: }

Neither is legal according to the spec, but the C# compiler allows both. Fortunately this is a pretty harmless bug.

In both cases the end point of the section is not reachable and so the empty section is reachable. But it is never the case that there is an empty section between cases.

Based on testing, you can do the following:

switch(i)
{
    case 0:
    case 1:
    case 2:
        doSomething();
        break;
    default:
        doNothing();
        break;
}

but you cannot:

switch(i)
{
   case 0:
      doSomething();
   case 1:
      doSomethingElse();
      break;
   default:
      doNothing();
      break;
}

So, the answer is, YES, you can stack multiple cases together, but you cannot let control run from one case to another.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top