Skip to content

Instantly share code, notes, and snippets.

@ThatRendle
Last active December 15, 2015 20:28
Show Gist options
  • Save ThatRendle/5318438 to your computer and use it in GitHub Desktop.
Save ThatRendle/5318438 to your computer and use it in GitHub Desktop.
Call-By-Ref vs Call-By-Value
IL_0001: ldstr "bar"
IL_0006: stloc.0 // str
IL_0007: ldarg.0
IL_0008: ldloc.0 // ldloc loads the variable onto the stack
IL_0009: call UserQuery.Foo
Foo:
IL_0000: nop
IL_0001: ldarg.1
IL_0002: callvirt System.String.ToUpper
IL_0007: starg.s 01
IL_0009: ldc.i4.1
IL_000A: stloc.0 // CS$1$0000
IL_000B: br.s IL_000D
IL_000D: ldloc.0 // CS$1$0000
IL_000E: ret
IL_0001: ldstr "bar"
IL_0006: stloc.0 // str
IL_0007: ldarg.0
IL_0008: ldloca.s 00 // ldloca loads the *address* of the variable onto the stack
IL_000A: call UserQuery.Foo
Foo:
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.1
IL_0003: ldind.ref // ldind gets the object reference from the passed address
IL_0004: callvirt System.String.ToUpper
IL_0009: stind.ref // stind stores the new object reference at the passed address
IL_000A: ldc.i4.1
IL_000B: stloc.0 // CS$1$0000
IL_000C: br.s IL_000E
IL_000E: ldloc.0 // CS$1$0000
IL_000F: ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment