title: Parallel.ForEach variables aren't treated as local
body: <p>I'm writing a file system searcher that takes directories (typically Windows drives) and filters as input and searches each input directory for files matching the input filters. Sequentially, I implemented it like so (simplified version of relevant code):</p>
  
  <pre class="lang-cs prettyprint-override"><code>
  foreach (var dir in Directories) {
      Console.WriteLine("Searching: " + dir);
      var s = new FSSearcher(dir, FileTypes, Keywords, SearchContents);
      s.Search();
  }
  
  class FSSearcher {
  
      [...]
      private static List&lt;string&gt; Filetypes;
      private static List&lt;string&gt; Keywords;
      private static List&lt;string&gt; Results;
      private static bool searchContents;
      private static string SearchDirectory;
      [...]
  
      public FSSearcher(string d, List&lt;string&gt; f, List&lt;string&gt; k, bool s) {
          SearchDirectory = d;
          Filetypes = f;
          Keywords = k;
          Results = new List&lt;string&gt;();
          searchContents = s;
      }
  
      public void Search() {
              if (Directory.Exists(SearchDirectory)) {
                  Console.WriteLine("Searching dir: " + SearchDirectory);
                  [...]
  
              }
      }
  }
  
  </code></pre>
  
  <p>This code works correctly and as expected. No results need to be collected at the end of the loop. FSSearcher is an "independent class". Now I want to run this <code>foreach</code> loop in parallel (different physical drives, so concurrency should not be throttled by I/O), so I changed the code above to the following (again simplified code): </p>
  
  <pre><code>var ConcurrentDirectories = new ConcurrentBag&lt;string&gt;();
  ConcurrentDirectories.Add("C:\\");
  ConcurrentDirectories.Add("Z:\\");
  var options = new ParallelOptions { MaxDegreeOfParallelism = ConcurrentDirectories.Count };
  Parallel.ForEach(ConcurrentDirectories, options, (dir) =&gt; {
          Console.WriteLine("Searching in parallel: " + dir);
          var s = new FSSearcher(dir, FileTypes, Keywords, SearchContents);
          s.Search();
  });
  </code></pre>
  
  <p>I'm assuming variable <code>s</code> is local to each iteration. However, this is not the case. The code above gives the following output:</p>
  
  <pre><code>Searching in parallel: Z:\
  Searching in parallel: C:\
  Searching dir: C:\
  Searching dir: C:\ &lt;=== this is expected to be Z:\
  </code></pre>
  
  <p>What appears to happen is that <code>s</code> is initialized for <code>Z:\</code> in one "iteration" and then re-assigned to another instance of FSSearcher for <code>C:\</code>. Then <code>Search()</code> is called twice for the last assigned object for <code>C:\</code>. </p>
  
  <p>I've read through all the related SO posts for an explanation or solution, but cannot seem to find an explanation. How is this happening and how can I prevent it from happening? </p>
  
  <p>Thanks a lot in advance! </p>