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
1.4m articles
1.4m replys
5 comments
57.0k users