As ever with such questions, the answer lies in the IL it generates. For the following code examples:
public int X()
{
{
{
{
return 0;
}
}
}
}
public int Y()
{
return 0;
}
We end up with the following compiled IL:
.method public hidebysig instance int32 X() cil managed
{
// Code size 2 (0x2)
.maxstack 8
IL_0000: ldc.i4.0
IL_0001: ret
} // end of method SomeType::X
.method public hidebysig instance int32 Y() cil managed
{
// Code size 2 (0x2)
.maxstack 8
IL_0000: ldc.i4.0
IL_0001: ret
} // end of method SomeType::Y
They are identical. so no, it has no affect on performance. X
is horrible to read, but that's another issue.
Update
{}
affects the scope of variables, so perhaps this could have an effect. Again, let's check:
public int X()
{
var i = 1;
{
{
i++;
{
return i;
}
}
}
}
public int Y()
{
var i = 1;
i++;
return i;
}
Once again though, the IL produced is identical:
// Code size 8 (0x8)
.maxstack 2
.locals init ([0] int32 i)
IL_0000: ldc.i4.1
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: ldc.i4.1
IL_0004: add
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: ret
However if the variable is captured in a closure, it does affect things. X
in the following case does create more IL, which would have an impact on performance:
public Func<int> X()
{
{
var i = 1;
{
i++;
{
return () => i;
}
}
}
}
public Func<int> Y()
{
var i = 1;
i++;
return () => i;
}