Using brackets after any if statement

In my new assignment, where we use .NET 3.5 I got the chance to use the ReSharper extensively. One of the new features I was surprised with is the usage of brackets after every if and else even when they contain simply one statement.
It occurred to me that the debugger stops on the brackets as a step in the processing cycle. So I decided to see what is the difference in the compiled IL code. Therefore I wrote the following code:


public class SeeTheDifference
{
public void Test1()
{
for (int i = 0; i < 10; i++)
{
TestMethod();
}
}
public void Test2()
{
for (int i = 0; i < 10; i++)
TestMethod();
}
private void TestMethod()
{
Debug.WriteLine("test");
}
}

And compiled in both Debug and in Release configuration. The result is the following snapshot in the Reflector:

.method public hidebysig instance void Test1() cil managed
{
.maxstack 2
.locals init (
[0] int32 i,
[1] bool CS$4$0000)
L_0000: nop
L_0001: ldc.i4.0
L_0002: stloc.0
L_0003: br.s L_0012
L_0005: nop
L_0006: ldarg.0
L_0007: call instance void Knowledgebase.SeeTheDifference::TestMethod()
L_000c: nop
L_000d: nop
L_000e: ldloc.0
L_000f: ldc.i4.1
L_0010: add
L_0011: stloc.0
L_0012: ldloc.0
L_0013: ldc.i4.s 10
L_0015: clt
L_0017: stloc.1
L_0018: ldloc.1
L_0019: brtrue.s L_0005
L_001b: ret
}
.method public hidebysig instance void Test2() cil managed
{
.maxstack 2
.locals init (
[0] int32 i,
[1] bool CS$4$0000)
L_0000: nop
L_0001: ldc.i4.0
L_0002: stloc.0
L_0003: br.s L_0010
L_0005: ldarg.0
L_0006: call instance void Knowledgebase.SeeTheDifference::TestMethod()
L_000b: nop
L_000c: ldloc.0
L_000d: ldc.i4.1
L_000e: add
L_000f: stloc.0
L_0010: ldloc.0
L_0011: ldc.i4.s 10
L_0013: clt
L_0015: stloc.1
L_0016: ldloc.1
L_0017: brtrue.s L_0005
L_0019: ret
}

As it is visible in this IL code, there are two nop commands as results of the bracket pair. According to Reflector “nop (0x0000): fills space if bytecodes are patched. No meaningful operation is performed although a processing cycle can be consumed.”

Author: Pouya Panahy

Microsoft certified DevOps engineer with passion in analysing, designing and implementing solutions for Azure Cloud with hands-on experience in security and quality assurence.

Leave a Reply