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?