As always with such questions, the answer lies in the IL that it generates. For the following code examples:
public int X() { { { { return 0; } } } } public int Y() { return 0; }
As a result, we get the following compiled IL:
.method public hidebysig instance int32 X() cil managed {
They are identical. so no, it does not affect performance. X is terrible to read, but this is another problem.
Updating {} affects the amount of variables, so perhaps this can have an effect. Again, check:
public int X() { var i = 1; { { i++; { return i; } } } } public int Y() { var i = 1; i++; return i; }
Once again, however, the IL produced is identical:
However, if a variable is locked, it affects things. X in the following case creates more IL, which will affect performance:
public Func<int> X() { { var i = 1; { i++; { return () => i; } } } } public Func<int> Y() { var i = 1; i++; return () => i; }
source share