title: Does the C# language compiler perform any actual optimizations on its own?
body: <p>Based on random Internet comments, I've always believed that the C# compiler does simple optimizations to the IL <em>(removing always-true if-statements, simple inlining, etc)</em>, then later the JIT performs the real, complex optimizations.</p>
  
  <p>As just one example, on <a href="https://msdn.microsoft.com/en-us/library/t0hfscdc.aspx">the documentation for the <code>/optimize</code> compiler flag</a>, it says</p>
  
  <blockquote>
    <p>The /optimize option enables or disables optimizations performed by the compiler <strong>to make your output file smaller</strong>, faster, and more efficient.</p>
  </blockquote>
  
  <p>which implies that at least <em>some</em> optimizations are applied by the language compiler.</p>
  
  <hr>
  
  <p>However, playing around with <a href="http://tryroslyn.azurewebsites.net">Try Roslyn</a>, this appears to be not true.  It looks like the C# compiler does literally no optimizations at all.</p>
  
  <h2>Examples</h2>
  
  <p>Input:</p>
  
  <pre><code>bool y = true;
  if (y)
      Console.WriteLine("yo");
  </code></pre>
  
  <p><a href="http://tryroslyn.azurewebsites.net/#f:r/K4Zwlgdg5gBAygTxAFwKYFsDcAoAxgGwEMQQYAHAJwHsoLD1sBvbGVmFQ5MXGANyrAATGOkKQAFAEoWbZm3kwARlSr4YCGAF4YyCsFQ4FbMADMY4hNKNGAwlQghVqAHQB1CmDQAZSKnEAiBCp/SUM2AF9scKAA==">Decompiled output:</a></p>
  
  <pre><code>if (true)
  {
      Console.WriteLine("yo");
  }
  </code></pre>
  
  <hr>
  
  <p>Input:</p>
  
  <pre><code>static void DoNothing() { }
  
  static void Main(string[] args)
  {
      DoNothing();
      Console.WriteLine("Hello world!");
  }
  </code></pre>
  
  <p><a href="http://tryroslyn.azurewebsites.net/#f:r/K4Zwlgdg5gBAygTxAFwKYFsDcAobBjAGwEMQQYA5AewCd0iCAFayqao9bAb2xl5hSLIweGADdKYACYwAIpSrIAFpCgAKAJQxOMAL48++3gKEjxUmAFkikVSmoqA2gF0YRalBDrDW731nzKJRUNHD8/AGFKCBBKAlQAOgB1ezQAGUhUVQAiAAlUAgJKGAB3GgJJAEIs9VC+PR0gAA">Decompiled output:</a></p>
  
  <pre><code>private static void DoNothing()
  {
  }
  private static void Main(string[] args)
  {
      NormalProgram.DoNothing();
      Console.WriteLine("Hello world!");
  }
  </code></pre>
  
  <hr>
  
  <p>Input:</p>
  
  <pre><code>try
  {
      throw new Exception();
  }
  catch (Exception)
  {
      Console.WriteLine("Hello world!");
  }
  </code></pre>
  
  <p><a href="http://tryroslyn.azurewebsites.net/#f:r/K4Zwlgdg5gBAygTxAFwKYFsDcAobBjAGwEMQQYA5AewCd0iCAFayqao9bAb2xl5hSLIweGADdKYACYwAskUgAKFNUhQA2gF0YRalBABKHn259TMZNQRGzMEzbPIAFswDuMCKjcBRAB55UAA5ClBAK+jj2MAC+1mZ4gniOMAq+/kFgIYaRdpG8AMIhIJQEqAB0AOoqaAAykKgKAEQAEqgEBJQwLjQEkgCEDeGxpjHD2FFAA==">Decompiled output:</a></p>
  
  <pre><code>try
  {
      throw new Exception();
  }
  catch (Exception)
  {
      Console.WriteLine("Hello world!");
  }
  </code></pre>
  
  <hr>
  
  <p>As you can see, the C# language compiler <strong>appears to do no optimizations at all</strong>.</p>
  
  <p>Is this true?  If so, why does the documentation claim that <code>/optimize</code> will make your executable smaller?</p>