Eric
Eric

Reputation: 2283

impact of bypassing variables

I don't know the right way to search for this question, but it's something I do a lot and wanted to figure out if I've been silently submarining my code all this time. Is something like this:

Int32 x = 5;
Int32 y = 7;
Int32 z = x+y;

Console.Write(z.ToString());

The same as this:

Int32 x = 5;
Int32 y = 7;

Console.Write((x+y).ToString());

I tend to write the first one much more than the second. It's more verbose, but I find debugging it a lot simpler because I can see what the result of a method is.

I'm building and deploying a .net 4.5 application, 64 bit.

Upvotes: 1

Views: 75

Answers (1)

Fredou
Fredou

Reputation: 20120

let look them, easy to figure that one out


you can see in both, debug/release build, both give pretty much the same result.

for the second method, it will create a temporary variable that will do exactly the same as your Int32 z = x+y; of your first method


DEBUG Method1 (First code snippet)

.method private hidebysig static 
    void Method1 () cil managed 
{
    // Method begins at RVA 0x2060
    // Code size 23 (0x17)
    .maxstack 2
    .locals init (
        [0] int32 x,
        [1] int32 y,
        [2] int32 z
    )

    IL_0000: nop
    IL_0001: ldc.i4.5
    IL_0002: stloc.0
    IL_0003: ldc.i4.7
    IL_0004: stloc.1
    IL_0005: ldloc.0
    IL_0006: ldloc.1
    IL_0007: add
    IL_0008: stloc.2
    IL_0009: ldloca.s z
    IL_000b: call instance string [mscorlib]System.Int32::ToString()
    IL_0010: call void [mscorlib]System.Console::Write(string)
    IL_0015: nop
    IL_0016: ret
} // end of method Program::Method1

DEBUG Method2 (Second code snippet)

.method private hidebysig static 
    void Method2 () cil managed 
{
    // Method begins at RVA 0x2084
    // Code size 23 (0x17)
    .maxstack 2
    .locals init (
        [0] int32 x,
        [1] int32 y,
        [2] int32 CS$0$0000
    )

    IL_0000: nop
    IL_0001: ldc.i4.5
    IL_0002: stloc.0
    IL_0003: ldc.i4.7
    IL_0004: stloc.1
    IL_0005: ldloc.0
    IL_0006: ldloc.1
    IL_0007: add
    IL_0008: stloc.2
    IL_0009: ldloca.s CS$0$0000
    IL_000b: call instance string [mscorlib]System.Int32::ToString()
    IL_0010: call void [mscorlib]System.Console::Write(string)
    IL_0015: nop
    IL_0016: ret
} // end of method Program::Method2

RELEASE Method1 (First code snippet)

.method private hidebysig static 
    void Method1 () cil managed 
{
    // Method begins at RVA 0x205c
    // Code size 21 (0x15)
    .maxstack 2
    .locals init (
        [0] int32 x,
        [1] int32 y,
        [2] int32 z
    )

    IL_0000: ldc.i4.5
    IL_0001: stloc.0
    IL_0002: ldc.i4.7
    IL_0003: stloc.1
    IL_0004: ldloc.0
    IL_0005: ldloc.1
    IL_0006: add
    IL_0007: stloc.2
    IL_0008: ldloca.s z
    IL_000a: call instance string [mscorlib]System.Int32::ToString()
    IL_000f: call void [mscorlib]System.Console::Write(string)
    IL_0014: ret
} // end of method Program::Method1

RELEASE Method2 (Second code snippet)

.method private hidebysig static 
    void Method2 () cil managed 
{
    // Method begins at RVA 0x2080
    // Code size 21 (0x15)
    .maxstack 2
    .locals init (
        [0] int32 x,
        [1] int32 y,
        [2] int32 CS$0$0000
    )

    IL_0000: ldc.i4.5
    IL_0001: stloc.0
    IL_0002: ldc.i4.7
    IL_0003: stloc.1
    IL_0004: ldloc.0
    IL_0005: ldloc.1
    IL_0006: add
    IL_0007: stloc.2
    IL_0008: ldloca.s CS$0$0000
    IL_000a: call instance string [mscorlib]System.Int32::ToString()
    IL_000f: call void [mscorlib]System.Console::Write(string)
    IL_0014: ret
} // end of method Program::Method2

Upvotes: 4

Related Questions