Does a C# compiler optimize programs by default?
<p>I have two "Hello World" programs:</p>

<pre><code>static void Main(string[] args) {
  Console.WriteLine("Hello World");
}
</code></pre>

<p>and </p>

<pre><code>static void Main(string[] args) {
  string hw = "Hello World";
  Console.WriteLine(hw);
}
</code></pre>

<p>and the IL code produced for each of these is:</p>

<pre><code>IL_0001:  ldstr       "Hello World"
IL_0006:  call        System.Console.WriteLine
</code></pre>

<p>and </p>

<pre><code>IL_0001:  ldstr       "Hello World"
IL_0006:  stloc.0     // hw
IL_0007:  ldloc.0     // hw
IL_0008:  call        System.Console.WriteLine
</code></pre>

<p>My question is, why did not, the C# compiler optimize this <strong>by default</strong>?</p>
