简体   繁体   中英

C# switch statement with curly braces for each case/default block within the switch statement?

Typically, a switch statement in C# looks like this

switch (sh)
{
   case 1:
      DoThis();
      DoThat();
      break;
   case 2:
      DoThis();
      DoThat();
      break;
   default:
      DoThis();
      DoThat();
      break;   
}

But for the first time ever, I saw someone using curly braces for each case statement within a switch statement like below:

switch (sh)
{
   case 1:
   {
      DoThis();
      DoThat();
      break;
   }
   case 2:
   {
      DoThis();
      DoThat();
      break;
   }
   default:
   {
      DoThis();
      DoThat();
      break; 
   }  
}

Why are these curly braces { , } being used for each case and default blocks of the case statement above?

Why are they needed?

What is the difference?

They are not required, but they are useful if you declare variables with the same name in multiple branches:

switch (sh) {
    case 1:
        var test = "test";
        Console.WriteLine(test);
        break;
    case 2:
        var test = "test";
        Console.WriteLine(test);
        break;
}

This will not compile, complaining about conflicting variable names. But if you add braces

switch (sh) {
    case 1: {
        var test = "";
        Console.WriteLine(test);
        break;
    }
    case 2: {
        var test = "";
        Console.WriteLine(test);
        break;
    }
}

that will create its own scope for each branch and it will compile fine.

Some people get used to this and always add bracers, even when not defining any variables.

The issue you sometimes face in a switch statement is that all the cases are in the same scope. That means (for example) that you can not use the same variable name in two cases. Consider this:

switch (sh) {
    case 1:
        int result = 1;
        return result;
    case 2:
        int result = 2;
        return result;
}

This will lead to a compiler error, since you are declaring result twice in the same scope. You remove the error by introducing new scopes in the cases:

switch (sh) {
    case 1: {
        int result = 1;
        return result;
    }
    case 2: {
        int result = 2;
        return result;
    }
}

Considering that switch cases are somewhat controversial, because they introduce additional complexity, adding extra scopes adds to the confusion. I prefer to use switch blocks in a way that does not lead to the described problem (by keeping the amount of code in the cases low and try to avoid large switch blocks).

In your example the scopes are not needed and I would not use them on a general basis, since this is "unusual" code. And unusual usually means confusing. Your question is a proof to that opinion (and it is an opinion, just to avoid a religious war here), since this construct confused you enough to ask the question.

I belive this is remains of old code. I remember in C when I didnt put the break I would just fall in the next case...

This is no longer relevant and is just code style.

Note you can also do

switch (sh)
{
   case 1:
      DoThis();
      DoThat();
      break;
   case 2:
   {
      DoThis();
      DoThat();
   }break; 
   case 3:
   case 4:
   {
      DoThis();
      DoThat();
   }
   case 5:
      goto case 1; 
   default:
   {
      DoThis();
      DoThat();
   }  
} 

The break will just leave the Switch but which will be left after leaving the bracket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM