0

Can I refer to a case in default instead of copy-pasting the whole code?

For example:

switch(n)
{
    case(1): //code here...
        break;
    case(2): //code here...
        break;
    case(3): //code here...
        break;
    default: case(2);
        break;
}
lenny
  • 734
  • 2
  • 15
  • 43
  • 2
    No, you can't. Why don't you just remove the `case(2)` and put the code in `default`? It's not necessary to list all options as `case`. – Ivan Stoev Aug 02 '16 at 08:25

5 Answers5

8

Just put it second to last, followed by default:

switch(n)
{
    case(1): //code here...
        break;
    case(3): //code here...
        break;
    case(2):
    default: //code here...
        break;
}

Or simply, not include it at all. If that case isn't hit, then it will fall back to the default anyway.

Blue
  • 22,608
  • 7
  • 62
  • 92
3

You can do like this. If there is no case, it will got to the default.

switch(n)
{
    case(1): //code here...
        break;
    case(3): //code here...
        break;
    default:
        break;
}

No need to do

case(2):
default: //code here...
     break;
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
1

If you want to refer default to case 2, you can omit case 2. Then every case 2 should jump to case default.

switch(n)
{
    case(1): //code here...
        break;

    case(3): //code here...
        break;

    default:  //case 2 should jump to this section because its not listed in your switch-cases
        break;
}
Seelixh
  • 77
  • 6
0

Something like this:

switch(n)
{
    case(1): //code here...
        break;
    case(3): //code here...
        break;
    case(2):
    default: 
        break;
}
thangcao
  • 1,819
  • 1
  • 13
  • 14
0

If you don't have any code in the default case, you can simply omit case 2 and move that code to the default, like the other answers say.

If you do have code that has to be run in the default case before you want to move on to case 2, you can use a goto. Here's an example using int:

int n = 5;
switch(n)
{
    case 1: //code here...
        break;
    case 2: //code here...
        break;
    case 3: //code here...
        break;
    default:
        //some code
        goto case 2;
}
Dennis_E
  • 8,751
  • 23
  • 29
  • 3
    Oh god no. [Never use `goto`](http://stackoverflow.com/questions/11906056/goto-is-this-bad). – Blue Aug 02 '16 at 10:04
  • this is the correct answer and this is the reason a goto statement exists in C#. – Daniel Fuchs Aug 02 '16 at 11:39
  • 1
    @DanielFuchs If the case for 2 is _exactly_ the same as default, then no, there is no reason whatsoever to do this. And goto should be avoided like the plague anyway. – Nyerguds Aug 02 '16 at 11:43
  • @FrankerZ Let's not hold this discussion here, but goto is not evil. (at least in C#). If you can't use goto, you shouldn't use break, continue or any other jump statement either. This is the standard way of doing this in C#. – Dennis_E Aug 02 '16 at 11:59
  • @Dennis_E I've seen the actual way switch-case is supposed to work on assembler level, and I'm fairly sure the c# implementation is hardly recognizable next to it. Switch-case, unlike for/while/if, actually exists as an elegant thing on bytecode level. – Nyerguds Aug 02 '16 at 12:04
  • This is terrible. Whomever changes case 2 in the future has to be aware they are changing the default as well. The code for case 2 should be wrapped in a method, and then called in case 2 and the default case instead. That would make the code much easier to understand and future updates much less risky. Down voting the answer. – Wes H Mar 13 '23 at 20:21