Why does C# allow code blocks without a preceding statement (e.g. if, else, for, while)?
if
else
for
while
void Main() { { // any sense in this? Console.Write("foo"); } }
The { ... } has at least the side-effect of introducing a new scope for local variables.
{ ... }
I tend to use them in switch statements to provide a different scope for each case and in this way allowing me to define local variable with the same name at closest possible location of their use and to also denote that they are only valid at the case level.
switch
2.1m questions
2.1m answers
60 comments
57.0k users