sometimes I see code like the following without actually knowing the meaning of it:
void myFunc() {
MyClass a = new MyClass();
{
if (a.b == null) // doSomething
}
}
I really like this kind of code-sorting and capsulation (not only because you may collapse the whole block at once if you won´t read its content), but I wonder if it has any syntactic meaning additional to just the optical indentation. Ofc. I know that like in every code-block any variable declared within the block is only usable there, but is there any more. There are also object-initializers but as those concern to an actual instance of a class the above is a totally independent block.
I wonder if it has any syntactic meaning additional to just the optical indentation
No, there's nothing special about them. The C# language declares a block as a pair of braces encapsulating an optional list of statements, and observes that this permits multiple statements to be written in places where a single statement is allowed.
This in turn simplifies other parts of the language specification, such as if, for, etc, which can then just be specified as if they're only ever followed by a single statement.
The fact that this means that you can also use them anywhere else is mostly a happy accident.
You can read the full specification for them in section 8.2 of the C# language spec
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With