It creates new scope for variables you used. Scope of variables can be tricky sometimes. For instance in the code you posted;
switch(x)
{
case(a):
{
int i = 0;
}
break;
case(b):
i = 1; // Error: The name 'i' doesn't exist in the current context
break;
}
The error makes sense here as in case(b)
variable a
is accessed out of scope. Now on the other hand,
switch(x)
{
case(a):
{
int i = 0;
}
break;
case(b):
int i = 1; // Error: A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else
break;
}
Above two errors look contradictory to each other. To get around this you should define the scope separately in both case statements,
switch(x)
{
case(a):
{
int i = 0;
}
break;
case(b):
{
int i = 1; // No error
}
break;
}
Eric Lippert shared a very good link to his blog to explain variable scopes in case statement. You should have a look at it.