title: Why can't I see captured variable behavior here?
body: <p>I encountered this strange behavior with captured variables. Below are two loops. I would expect both to behave the same. Why it behaves differently?</p>
  
  <pre><code>        private static void Loop1()
      {
          var actions = new List&lt;Action&gt;();
          foreach (var number in Enumerable.Range(1, 5))
          {
              actions.Add(()=&gt;Console.WriteLine(number));
          }
          foreach (var currentAction in actions)
          {
              currentAction();
          }
      }
  
      private static void Loop2()
      {
          var actions = new List&lt;Action&gt;();
          for (int number = 0; number &lt;= 4; number++)
          {
              actions.Add(() =&gt; Console.WriteLine(number));
          }
          foreach (var currentAction in actions)
          {
              currentAction();
          }
      }
  </code></pre>
  
  <p>The first loops print 1,2,3,4,5
  The second loop prints 5,5,5,5,5</p>