c# - Is return variable automatically created by compiler? -


when going on project source code, stumbled upon method , wondered 1 thing. following 2 methods same performance/memory/compiler point of view?

public static string foo(string inputvar) {     string bar = dosomething(inputvar);     return bar; }  public static string foo(string inputvar) {     return dosomething(inputvar); } 

is return variable automatically created compiler?

using il disassembler (included in .net sdk/vs) can @ il generated compiler. code generated using vs2013 (not roslyn).

the top 1 gives following il:

.method public hidebysig static string  foo(string inputvar) cil managed {   // code size       14 (0xe)   .maxstack  1   .locals init ([0] string bar,            [1] string cs$1$0000)   il_0000:  nop   il_0001:  ldarg.0   il_0002:  call       string testil.program::dosomething(string)   il_0007:  stloc.0   il_0008:  ldloc.0   il_0009:  stloc.1   il_000a:  br.s       il_000c   il_000c:  ldloc.1   il_000d:  ret } // end of method program::foo 

the second one:

.method public hidebysig static string  foo(string inputvar) cil managed {   // code size       12 (0xc)   .maxstack  1   .locals init ([0] string cs$1$0000)   il_0000:  nop   il_0001:  ldarg.0   il_0002:  call       string testil.program::dosomething(string)   il_0007:  stloc.0   il_0008:  br.s       il_000a   il_000a:  ldloc.0   il_000b:  ret } // end of method program::foo 

the difference seems first 1 creates entry in methods locals table. if optimized away jit compiler don't know.

to answer question: no, doesn't seems compiler automatically generates local variable in case, in more advanced case might (like return x * (y + z)).

edit: if turn on "optimize code" more clear:

.method public hidebysig static string  foo(string inputvar) cil managed {   // code size       9 (0x9)   .maxstack  1   .locals init ([0] string bar)   il_0000:  ldarg.0   il_0001:  call       string testil.program::dosomething(string)   il_0006:  stloc.0   il_0007:  ldloc.0   il_0008:  ret } // end of method program::foo  .method public hidebysig static string  foo(string inputvar) cil managed {   // code size       7 (0x7)   .maxstack  8   il_0000:  ldarg.0   il_0001:  call       string testil.program::dosomething(string)   il_0006:  ret } // end of method program::foo 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -