{"id":71,"date":"2010-06-11T11:43:00","date_gmt":"2010-06-11T11:43:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2010\/06\/11\/using-brackets-after-any-if-statement\/"},"modified":"2010-06-11T11:43:00","modified_gmt":"2010-06-11T11:43:00","slug":"using-brackets-after-any-if-statement","status":"publish","type":"post","link":"http:\/\/www.panahy.nl\/index.php\/2010\/06\/11\/using-brackets-after-any-if-statement\/","title":{"rendered":"Using brackets after any if statement"},"content":{"rendered":"<p>In my new assignment, where we use <span style=\"font-weight:bold;\">.NET 3.5<\/span> 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.<br \/>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:<\/p>\n<pre><code><br \/>  public class SeeTheDifference<br \/>  {<br \/>    public void Test1()<br \/>    {<br \/>      for (int i = 0; i < 10; i++)<br \/>      <span style=\"font-weight:bold; color:blue;\">{<\/span><br \/>        TestMethod();<br \/>      <span style=\"font-weight:bold; color:blue;\">}<\/span><br \/>    }<br \/>    public void Test2()<br \/>    {<br \/>      for (int i = 0; i < 10; i++)<br \/>        TestMethod();      <br \/>    }<br \/>    private void TestMethod()<br \/>    {<br \/>      Debug.WriteLine(\"test\");<br \/>    }<br \/>  }<br \/><\/code><\/pre>\n<p>And compiled in both Debug and in Release configuration. The result is the following snapshot in the Reflector:<\/p>\n<pre><code>.method public hidebysig instance void Test1() cil managed<br \/>{<br \/>    .maxstack 2<br \/>    .locals init (<br \/>        [0] int32 i,<br \/>        [1] bool CS$4$0000)<br \/>    L_0000: nop <br \/>    L_0001: ldc.i4.0 <br \/>    L_0002: stloc.0 <br \/>    L_0003: br.s L_0012<br \/>    <span style=\"font-weight:bold; color:blue;\">L_0005: nop <\/span><br \/>    L_0006: ldarg.0 <br \/>    L_0007: call instance void Knowledgebase.SeeTheDifference::TestMethod()<br \/>    <span style=\"font-weight:bold; color:blue;\">L_000c: nop<\/span> <br \/>    L_000d: nop <br \/>    L_000e: ldloc.0 <br \/>    L_000f: ldc.i4.1 <br \/>    L_0010: add <br \/>    L_0011: stloc.0 <br \/>    L_0012: ldloc.0 <br \/>    L_0013: ldc.i4.s 10<br \/>    L_0015: clt <br \/>    L_0017: stloc.1 <br \/>    L_0018: ldloc.1 <br \/>    L_0019: brtrue.s L_0005<br \/>    L_001b: ret <br \/>}<br \/>.method public hidebysig instance void Test2() cil managed<br \/>{<br \/>    .maxstack 2<br \/>    .locals init (<br \/>        [0] int32 i,<br \/>        [1] bool CS$4$0000)<br \/>    L_0000: nop <br \/>    L_0001: ldc.i4.0 <br \/>    L_0002: stloc.0 <br \/>    L_0003: br.s L_0010<br \/>    L_0005: ldarg.0 <br \/>    L_0006: call instance void Knowledgebase.SeeTheDifference::TestMethod()<br \/>    L_000b: nop <br \/>    L_000c: ldloc.0 <br \/>    L_000d: ldc.i4.1 <br \/>    L_000e: add <br \/>    L_000f: stloc.0 <br \/>    L_0010: ldloc.0 <br \/>    L_0011: ldc.i4.s 10<br \/>    L_0013: clt <br \/>    L_0015: stloc.1 <br \/>    L_0016: ldloc.1 <br \/>    L_0017: brtrue.s L_0005<br \/>    L_0019: ret <br \/>}<br \/><\/code><\/pre>\n<p>As it is visible in this IL code, there are two nop commands as results of the bracket pair. According to Reflector \u201cnop (0x0000): fills space if bytecodes are patched. No meaningful operation is performed although a processing cycle can be consumed.\u201d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"http:\/\/www.panahy.nl\/index.php\/2010\/06\/11\/using-brackets-after-any-if-statement\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using brackets after any if statement&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[43],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/www.panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"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&hellip;","_links":{"self":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/71"}],"collection":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":0,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}