title: C# *Strange* problem with StopWatch and a foreach loop
body: <p>I have the this code:</p>
  
  <pre><code>var options = GetOptions(From, Value, SelectedValue);     
  var stopWatch = System.Diagnostics.Stopwatch.StartNew();
  
  foreach (Option option in options)
  {
      stringBuilder.Append("&lt;option");
  
      stringBuilder.Append(" value=\"");
      stringBuilder.Append(option.Value);
      stringBuilder.Append("\"");
  
      if (option.Selected)
          stringBuilder.Append(" selected=\"selected\"");
  
      stringBuilder.Append('&gt;');
  
      stringBuilder.Append(option.Text);
      stringBuilder.Append("&lt;/option&gt;");
  }
  
  HttpContext.Current.Response.Write("&lt;b&gt;" + stopWatch.Elapsed.ToString() + "&lt;/b&gt;&lt;br&gt;");
  </code></pre>
  
  <p>It is writing:<br>
   00:00:00.0004255 in the first try (not in debug)<br>
   00:00:00.0004260 in the second try and<br>
   00:00:00.0004281 in the third try.</p>
  
  <p>Now, if I change the code so the measure will be <em>inside</em> the foreach loop:</p>
  
  <pre><code>var options = GetOptions(From, Value, SelectedValue);     
  
  foreach (Option option in options)
  {
      var stopWatch = System.Diagnostics.Stopwatch.StartNew();
  
      stringBuilder.Append("&lt;option");
  
      stringBuilder.Append(" value=\"");
      stringBuilder.Append(option.Value);
      stringBuilder.Append("\"");
  
      if (option.Selected)
          stringBuilder.Append(" selected=\"selected\"");
  
      stringBuilder.Append('&gt;');
  
      stringBuilder.Append(option.Text);
      stringBuilder.Append("&lt;/option&gt;");
  
      HttpContext.Current.Response.Write("&lt;b&gt;" + stopWatch.Elapsed.ToString() + "&lt;/b&gt;&lt;br&gt;");
  }
  </code></pre>
  
  <p>...I get<br>
  [00:00:00.0000014, 00:00:00.0000011] = 00:00:00.0000025  in the first try (not in debug),<br>
  [00:00:00.0000016, 00:00:00.0000011] = 00:00:00.0000027 in the second try and<br>
  [00:00:00.0000013, 00:00:00.0000011] = 00:00:00.0000024  in the third try.</p>
  
  <p>?!<br>
  It is completely unsense according to the first results... I've heard that the <code>foreach</code> loop is slow, but never imagined that it is <strong>so</strong> slow... Is it that?</p>
  
  <p><code>options</code> has 2 options.
  Here's the <code>option</code> class, if it is needed:</p>
  
  <pre><code>public class Option
  {
      public Option(string text, string value, bool selected)
      {
          Text = text;
          Value = value;
          Selected = selected;
      }
  
      public string Text
      {
          get;
          set;
      }
  
      public string Value
      {
          get;
          set;
      }
  
      public bool Selected
      {
          get;
          set;
      }
  }
  </code></pre>
  
  <p>Thanks.</p>