2

Take as an example the following C# function:

    static void Main(string[] args)
    {
        var r = new Random();
        {
            var i = r.Next(); ;
            Console.WriteLine("i = {0}", i);
        }

        var action = new Action(delegate()
            {
                var i = r.Next();
                Console.WriteLine("Delegate: i = {0}", i);
            });
        action();
    }

The following block only exists as C# syntactic sugar to enforce an extra layer of variable scope in the source code, as discussed in this SO Question.

        {
            var i = r.Next(); ;
            Console.WriteLine("i = {0}", i);
        }

I proved this by decompiling the generated assembly with ILSpy and getting this:

    private static void Main(string[] args)
    {
        Random r = new Random();
        int i = r.Next();
        Console.WriteLine("i = {0}", i);
        Action action = delegate
        {
            int j = r.Next();
            Console.WriteLine("Delegate: i = {0}", j);
        }
        ;
        action();
    }

So does this C# construct have a name? If so what is it?

Community
  • 1
  • 1
Justin Dearing
  • 14,270
  • 22
  • 88
  • 161
  • 2
    I'd call it a block, and they are also used in C, eg see http://stackoverflow.com/questions/1677778/why-enclose-blocks-of-c-code-in-curly-braces – fvu Jul 08 '12 at 22:20
  • I wonder what standard they were introduced in. – Justin Dearing Jul 08 '12 at 22:21
  • in C you mean? Can't tell you for sure but the first time I saw them being used predates even ANSI C I think. – fvu Jul 08 '12 at 22:22
  • @fvu Really? I'd figure that would be a C99 thing. Isn't that when C finally let you declare variables mid code. – Justin Dearing Jul 08 '12 at 22:24
  • check the accepted answer to question I linked before, this construct was actually used to bypass that restriction in older dialects. And I think it was in the output of some odd CASE tool that converted Nassi-Shneiderman flowcharts to something that looked more or less like C that I saw them first I think, probably because it reduced the scope the tool had to take into account to manage the vars it generated. – fvu Jul 08 '12 at 22:29

2 Answers2

6

It's called a statement block.

There is actually no difference between a statement block after e.g. an if (...) and a statement block that stands alone. A statement block can be used everywhere where a statement can be used.

dtb
  • 213,145
  • 36
  • 401
  • 431
  • @JustinDearing: Any source for "unnamed statement block"? A quick google search returns about 5 results, and none are related to C#. There is really nothing special about a statement block that is not used within statement like `if` or `while`, so I don't think there is a specific name. – dtb Jul 08 '12 at 22:38
2

I'm not aware of a specific name for the { } used in that specific context, but the name of the thing they introduce is called nested scope. (Reference also usage of nested scope in the documentation for goto).

Eric J.
  • 147,927
  • 63
  • 340
  • 553