title: C# Parallel Foreach using local and non-local variables
body: <p>I’m having some trouble understating how to make variables local using parallel threads. In a nutshell, I have a list of <code>SingleRecord</code>s called <code>RecordList</code>. With some logic based on the <code>SingleRecord</code> variable I have some local variables.  I was hoping to have each parallel thread have its own <code>IntVal</code> and <code>StringVal</code> and then add those to a table that all parallel threads can access. Been reading some on this but am not sure I fully understand quite how it works. </p>
  
  <pre><code>DataSet MyDataSet = new DataSet();
  DataTable MyTable = MyDataSet.Tables.Add();
  
  MyTable.Columns.Add("NumberVal", typeof(int));
  MyTable.Columns.Add("TextVal", typeof(string));
  
  Parallel.ForEach(RecordList, SingleRecord =&gt;
  {
      int IntVal = 0;
      string StringVal = string.Empty;
  
      IntVal = some logic;
      StringVal = SingleRecord + some logic; 
  
      MyTable.Rows.Add(IntVal, StringVal);
   });
  </code></pre>