title: Getting Collection was modified; enumeration operation may not execute. exception
body: <p>Getting Collection was modified; enumeration operation may not execute. exception</p>
  
  <p>Code:</p>
  
  <pre><code>public static string GetValue(List&lt;StateBag&gt; stateBagList, string name)
  {
      string retValue = string.Empty;
  
      if (stateBagList != null)
      {
          foreach (StateBag stateBag in stateBagList)
          {
              if (stateBag.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
              {
                  retValue = stateBag.Value;
              }
          }
      }
  
      return retValue;
  }
  </code></pre>
  
  <p>getting this exception some time times not every time at this place.</p>
  
  <p>stacktrace:</p>
  
  <blockquote>
    <p>at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource
    resource)</p>
    
    <p>at System.Collections.Generic.List`1.Enumerator.MoveNextRare()</p>
    
    <p>at System.Collections.Generic.List`1.Enumerator.MoveNext()</p>
    
    <p>at Tavisca.TravelNxt.Shared.Entities.StateBag.GetValue(List`1 stateBagList, String name)</p>
  </blockquote>
  
  <hr>
  
  <p>@no one i have tried for following code but still getting exception</p>
  
  <p>code:</p>
  
  <pre><code> class StateBag
  {
      public string Name;
      public string Value;
  }
  
  class Program
  {
      static List&lt;StateBag&gt; _concurrent = new List&lt;StateBag&gt;();
  
      static void Main()
      {
          var sw = new Stopwatch();
          try
          {
              sw.Start();
              Thread thread1 = new Thread(new ThreadStart(A));
              Thread thread2 = new Thread(new ThreadStart(B));
              thread1.Start();
              thread2.Start();
              thread1.Join();
              thread2.Join();
              sw.Stop();
          }
          catch (Exception ex)
          {
          }
  
  
          Console.WriteLine("Average: {0}", sw.ElapsedTicks);
          Console.ReadKey();
      }
  
      private static Object thisLock = new Object();
  
      public static string GetValue(List&lt;StateBag&gt; stateBagList, string name)
      {
          string retValue = string.Empty;
  
  
          if (stateBagList != null)
          {
              lock (thisLock)
              {
                  foreach (StateBag stateBag in stateBagList)
                  {
                      if (stateBag.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                      {
                          retValue = stateBag.Value;
                      }
                  }
              }
  
          }
  
  
  
          return retValue;
      }
  
      static void A()
      {
          for (int i = 0; i &lt; 5000; i++)
          {
              _concurrent.Add(new StateBag() { Name = "name" + i, Value = i.ToString() });
          }
      }
  
      static void B()
      {
          for (int i = 0; i &lt; 5000; i++)
          {
              var t = GetValue(_concurrent, "name" + i);
          }
      }
  }
  </code></pre>