Consider this class:
public class Foo
{
// Fields
private string _bar;
// Properties
private string Bar
{
get
{
return this._bar;
}
set
{
this._bar = value;
}
}
}
Now when I go and look in the IL code emitted by the compiler for the setter of the Bar
property:
.method private hidebysig specialname instance void set_Bar(string 'value') cil managed
{
.maxstack 8
L_0000: nop
L_0001: ldarg.0
L_0002: ldarg.1
L_0003: stfld string ConsoleApplication2.Program/Foo::_bar
L_0008: ret
}
Why does it do a ldarg.0
? WHAT is located in first (index 0) argument? Since the method/property setter only takes 1 argument...
The same goes for the getter:
.method private hidebysig specialname instance string get_Bar() cil managed
{
.maxstack 1
.locals init (
[0] string CS$1$0000)
L_0000: nop
L_0001: ldarg.0
L_0002: ldfld string ConsoleApplication2.Program/Foo::_bar
L_0007: stloc.0
L_0008: br.s L_000a
L_000a: ldloc.0
L_000b: ret
}
Why the .locals init
? Why the ldarg.0 ? Why doesn't it do a ldfld
of the backing field and just return that? :)
For the setter:
Any instance member has an implicit "this" parameter - that's what's being loaded, basically. Try turning it into a static property and you'll see it go away.
For the getter, I'm not sure why there's the local variable... debugger support perhaps? Certainly compiling it in optimized mode (/o+ /debug-
from the command line) gets rid of the local variable.