2

After reading answers to this question, one thing is unclear to me. User David Arno states that in the following situation the compiler will generate more IL code for method X, because of additional curly braces.

public Func<int> X()
{
    {
        var i = 1;
        {
            i++;
            {
                return () => i;
            }
        }
    }
}

public Func<int> Y()
{
    var i = 1;
    i++;
    return () => i;
}

I can't see the difference between those two pieces of code. To me it looks like you could safely remove all the braces from X. Which pair of braces is responsible for the additional IL code? I think the snippet would be clearer if X had just one pair of braces more than Y (I assume that it would be enough to prove the point).

Community
  • 1
  • 1
Kapol
  • 6,383
  • 3
  • 21
  • 46
  • I don't interpret David's answer as saying that Y doesn't capture the local variable. – Jon Skeet Jul 20 '15 at 15:46
  • Ahhh, all right. That makes sense. I'll edit my question. Thanks. – Kapol Jul 20 '15 at 15:48
  • At least in Roslyn, that isn't true at all. http://tryroslyn.azurewebsites.net/#b:preview/f:s/K4Zwlgdg5gBAygTxAFwKYFsDcAobAHYAIwBswBjGAMWAjIB5JkA+GADQAoBKbAb2xgEw+gkTABuAQwBOMMDAC8MAIw5Rg4WtFgA1NtWaRGg2qmpkwKRBhcFLMPuMwAvv00uRLl/iKkK1WgwQzDAAmly8rgKSMnKKKpGyug4wpuaW1py2sjhOQAAA – SLaks Jul 20 '15 at 15:50
  • I have edited my question. I hope that it makes more sense now. – Kapol Jul 20 '15 at 15:52
  • 1
    Its very simple to create this program, compile it, then open it with ILDASM and view the output. It would also help if you added the output here or some portion of it and asked something specific. Maybe when you look at it, it will become clear... – Ron Beyer Jul 20 '15 at 16:02
  • It's hard to tell which pair of braces would be responsible for the IL, but judging by David's answer, if a variable is captured inside of additional braces it will generate more IL. So in this case it would be the second pair of brackets. I get that you're just trying to understand how the compiler creates IL, but as far as coding, the extra useless braces are terrible for readability. – DrZoo Jul 20 '15 at 16:02

1 Answers1

0

I compiled the code and checked the resulting IL with ILDASM. It turns out that in this scenario there is no difference in generated code for both methods.

.method public hidebysig instance int32  '<X>b__0'() cil managed
{
  // Code size       11 (0xb)
  .maxstack  1
  .locals init ([0] int32 CS$1$0000)
  IL_0000:  ldarg.0
  IL_0001:  ldfld      int32 ConsoleApplication1.Test/'<>c__DisplayClass2'::i
  IL_0006:  stloc.0
  IL_0007:  br.s       IL_0009
  IL_0009:  ldloc.0
  IL_000a:  ret
} // end of method '<>c__DisplayClass2'::'<X>b__0'


.method public hidebysig instance int32  '<Y>b__4'() cil managed
{
  // Code size       11 (0xb)
  .maxstack  1
  .locals init ([0] int32 CS$1$0000)
  IL_0000:  ldarg.0
  IL_0001:  ldfld      int32 ConsoleApplication1.Test/'<>c__DisplayClass5'::i
  IL_0006:  stloc.0
  IL_0007:  br.s       IL_0009
  IL_0009:  ldloc.0
  IL_000a:  ret
} // end of method '<>c__DisplayClass5'::'<Y>b__4'
Kapol
  • 6,383
  • 3
  • 21
  • 46