3

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.

ale
  • 10,012
  • 5
  • 40
  • 49
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • 4
    As far as I know, the only syntactic meaning of a "free" block is to scope variables. – Jodrell Sep 23 '14 at 07:37
  • haven't really seen this ... interesting though ... – Noctis Sep 23 '14 at 07:37
  • Outside from what you mention yourself of scope - there should be no other meaning – Allan S. Hansen Sep 23 '14 at 07:37
  • I would assume that apart from scoping variables, there's also the implied possibility of conditionally transferring control to the end of the block using a `break` statement? – Wim Ombelets Sep 23 '14 at 07:41
  • @WimOmbelets, your comment is considered harmful. http://en.wikipedia.org/wiki/Considered_harmful – Jodrell Sep 23 '14 at 07:43
  • 1
    @WimOmbelets - no, `break` is explicitly linked to an enclosing statement (`switch`, `while`, `do`, `for`, or `foreach`) and is defined to produce a compile-time error if it's not so enclosed. See section 8.9.1 of the language spec (link in my answer) – Damien_The_Unbeliever Sep 23 '14 at 07:44
  • http://stackoverflow.com/questions/249009/do-you-use-curly-braces-for-additional-scoping – CodeCaster Sep 23 '14 at 07:47

2 Answers2

3

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

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
0

Builds like the one you describe

void myFunc() {
    MyClass a = new MyClass();

    {
        if (a.b == null) // doSomething
    }
}

Are mainly used for the same thing why you like them. Just for optical reasons and being able to collapse those parts. They CAN also be used for variable scoping but that is less seldomly used than for the other purpose.

A possible alternative to this (taking only the optical reasons into account) would be the usage of regions to achieve the same result. Although it has the disadvantage that you have to write more than a mere {} you have the advantage that you can declare WHAT the code part is about.

void myFunc() {
    MyClass a = new MyClass();

    #region Setting the players base stats
        if (a.b == null) // doSomething
    #endregion
}

This approach does nothing for variable scoping but gives you at least some idea what it is that is being not shown (if the region is collapsed). Also both methods can be combined (thus a {} inside the region so that you can encapsulate variables).

void myFunc() {
    MyClass a = new MyClass();

    #region Setting the players base stats
    {
        if (a.b == null) // doSomething
    }
    #endregion
}

But back to your question aside from the optical reasons and the variable scopes there are no syntactical reasons to do this.

Thomas
  • 2,886
  • 3
  • 34
  • 78