C# Switch statement with/without curly brackets.... what's the difference?

Has C# always permitted you to omit curly brackets inside a switch() statement between the case: statements?

What is the effect of omitting them, as javascript programmers often do?

Example:

switch(x)
{
case OneWay:
{                               //  <---- Omit this entire line
int y = 123;
FindYou(ref y);
break;
}                               //  <---- Omit this entire line
case TheOther:
{                               //  <---- Omit this entire line
double y = 456.7; // legal!
GetchaGetcha(ref y);
break;
}                               //  <---- Omit this entire line
}
29827 次浏览

The curly braces are an optional part of the switch block, they are not part of the switch sections. Braces can be inserted within switch sections or equally inserted anywhere to control scope in your code.

They can be useful to limit scope within the switch block. For example:

int x = 5;


switch(x)
{
case 4:
int y = 3;
break;
case 5:
y = 4;
//...
break;
}

vs...

int x = 5;


switch(x)
{
case 4:
{
int y = 3;
break;
}
case 5:
{
y = 4;//compiling error
//...
break;
}
}

Note: C# will require you to set a value to y within the case 5 block in the first example before using it. This is protection against accidental variable follow through.

They are not strictly necessary but as soon as you start declaring local variables (in the switch branches) they are very much recommended.

This won't work:

    // does not compile
switch (x)
{
case 1 :
int j = 1;
...
break;


case 3:
int j = 3;   // error
...
break;
}

This compiles but it's spooky:

    switch (x)
{
case 1 :
int j = 1;
...
break;


case 3:
j = 3;
...
break;
}

So this is best:

  switch (x)
{
case 1 :
{
int j = 1;
...
}
break;  // I prefer the break outside of the  { }


case 3:
{
int j = 3;
...
}
break;
}

Just keep it simple and readable. You don't want to require readers to have a detailed knowledge of the rules involved in the middle example.

Curly braces are not required, but they might come in handy to introduce a new declaration space. This behavior hasn't changed since C# 1.0 as far as I know.

The effect of omitting them is that all variables declared somewhere inside the switch statement are visible from their point of declaration throughout all case branches.

See also Eric Lippert's example (case 3 in the post):

Four switch oddities

Eric's example:

switch(x)
{
case OneWay:
int y = 123;
FindYou(ref y);
break;
case TheOther:
double y = 456.7; // illegal!
GetchaGetcha(ref y);
break;
}

This does not compile because int y and double y are in the same declaration space introduced by the switch statement. You can fix the error by separating the declaration spaces using braces:

switch(x)
{
case OneWay:
{
int y = 123;
FindYou(ref y);
break;
}
case TheOther:
{
double y = 456.7; // legal!
GetchaGetcha(ref y);
break;
}
}

The braces inside the switch is actually not part of the switch structure at all. They are just scope blocks, which you can apply in code anywhere you like.

The difference between having them and not having them is that each block has it's own scope. You can declare local variables inside the scope, that doesn't conflict with other variables in another scope.

Example:

int x = 42;
{
int y = x;
}
{
int y = x + 1; // legal, as it's in a separate scope
}