title: How to tell a lambda function to capture a copy instead of a reference in C#?
body: <p>I've been learning C#, and I'm trying to understand lambdas.  In this sample below, it prints out 10 ten times.</p>
  
  <pre><code>class Program
  {
      delegate void Action();
      static void Main(string[] args)
      {
          List&lt;Action&gt; actions = new List&lt;Action&gt;();
  
          for (int i = 0; i &lt; 10; ++i )
              actions.Add(()=&gt;Console.WriteLine(i));
  
          foreach (Action a in actions)
              a();
      }
  }
  </code></pre>
  
  <p>Obviously, the generated class behind the lambda is storing a reference or pointer to the <code>int i</code> variable, and is assigning a new value to the same reference every time the loop iterates.  Is there a way to force the lamda to grab a copy instead, like the C++0x syntax</p>
  
  <pre><code>[&amp;](){ ... } // Capture by reference
  </code></pre>
  
  <p>vs.</p>
  
  <pre><code>[=](){ ... } // Capture copies
  </code></pre>