I’m working with IL code now and will need to write by myself in the future. There are some concerns I have because of misunderstanding. Here is a simple method in C#
public static string Method1(int id)
{
return Method2(id);
}
and here is its IL code
.method public hidebysig static string
Method1(int32 id) cil managed
{
//
.maxstack 1
.locals init ([0] string CS$1$0000)
IL_0000: nop // Why?
IL_0001: ldarg.0
IL_0002: call string MyNamespace.MyClass::Method2(int32)
IL_0007: stloc.0 // storing a return value of MyClass::Method2 to local variable.
IL_0008: br.s IL_000a // Why?
IL_000a: ldloc.0 // Does it really require and why?
IL_000b: ret
} // end of method MyClass::Method1
Each method in CIL has nop operation for some reason. Why does it have it?
In my case, is it necessary to use br.s IL_000a and will it working without it?
The nops are there to allow you to debug (set breakpoint), if you compile in release mode you will see less nopes in your code.
If you compile the same code in release/optimized mode, the IL should look much saner: