Suppose I have the following code:
public class Class1
{
private Class2 obj;
public void MethodA()
{
var class2 = new Class2();
class2.PropertyI = 2;
obj = MethodB(class2);
}
public Class2 MethodB(Class2 class2)
{
return class2;
}
}
public class Class2
{
public int PropertyI { get; set; }
}
The generated IL code from compiling with Visual Studio 2010 as a .NET 2.0 assembly is the following:
.method public hidebysig instance void MethodA() cil managed
{
.maxstack 3
.locals init (
[0] class ClassLibrary1.Class2 class2)
L_0000: nop
L_0001: newobj instance void ClassLibrary1.Class2::.ctor()
L_0006: stloc.0
L_0007: ldloc.0
L_0008: ldc.i4.2
L_0009: callvirt instance void ClassLibrary1.Class2::set_PropertyI(int32)
L_000e: nop
L_000f: ldarg.0
L_0010: ldarg.0
L_0011: ldloc.0
L_0012: call instance class ClassLibrary1.Class2 ClassLibrary1.Class1::MethodB(class ClassLibrary1.Class2)
L_0017: stfld class ClassLibrary1.Class2 ClassLibrary1.Class1::obj
L_001c: ret
}
.method public hidebysig instance class ClassLibrary1.Class2 MethodB(class ClassLibrary1.Class2 class2) cil managed
{
.maxstack 1
.locals init (
[0] class ClassLibrary1.Class2 CS$1$0000)
L_0000: nop
L_0001: ldarg.1
L_0002: stloc.0
L_0003: br.s L_0005
L_0005: ldloc.0
L_0006: ret
}
My questions are the following:
- In MethodA, why is there not a
nop
code betweenL_0006
andL_0007
?- Since
L_0001
toL_0006
are distinct fromL_0007
toL_0009
, why is there nonop
opcode?
- Since
- In MethodB, why is
L_0003
necessary?