Why does C# allow {} code blocks without a preceding statement?

Why does C# allow code blocks without a preceding statement (e.g. 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.


In the context you give, there is no significance. Writing a constant string to the console is going to work the same way anywhere in program flow.1

Instead, you typically use them to restrict the scope of some local variables. This is further elaborated here and here. Look at João Angelo’s answer and Chris Wallis’s answer for brief examples. I believe the same applies to some other languages with C-style syntax as well, not that they’d be relevant to this question though.


1Unless, of course, you decide to try to be funny and create your own Console class, with a Write() method that does something entirely unexpected.