Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Performance efficiency - more efficient but only by a few instructions, storing and loading the variable from the execution stack. Bear in mind that the Jitter may optimise your code at run-time, removing any unnecessary instructions.</p> <p>Memory efficiency - more efficient as there is no local var defined on the execution stack.</p> <p>Code clarity - having the definition of a variable closer to where it's used makes the code more usable. In my opinion having the one method returning into another is even cleaner however does have the downside that it's harder to inspect the value being returned (in VS 2013 this will be possible however).</p> <p>IL with the temp variable:</p> <pre><code> // Code size 13 (0xd) .maxstack 1 .locals init ([0] int32 myVar) IL_0000: call int32 StackOverflowScratchPad.Program::FunctionThatReturnsAnInteger() IL_0005: stloc.0 IL_0006: ldloc.0 IL_0007: call void [mscorlib]System.Console::WriteLine(int32) IL_000c: ret </code></pre> <p>and without:</p> <pre><code> // Code size 11 (0xb) .maxstack 8 IL_0000: call int32 StackOverflowScratchPad.Program::FunctionThatReturnsAnInteger() IL_0005: call void [mscorlib]System.Console::WriteLine(int32) IL_000a: ret </code></pre> <p>Bonus - IL when assigning an value to temp var at the declaration:</p> <pre><code> // Code size 15 (0xf) .maxstack 1 .locals init ([0] int32 myVar) IL_0000: ldc.i4.5 IL_0001: stloc.0 IL_0002: call int32 StackOverflowScratchPad.Program::FunctionThatReturnsAnInteger() IL_0007: stloc.0 IL_0008: ldloc.0 IL_0009: call void [mscorlib]System.Console::WriteLine(int32) IL_000e: ret </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload