title: Dictionary<string, bool?> error - Collection was modified; enumeration operation may not execute
body: <p>I am getting <strong>System.InvalidOperationException: Collection was modified; enumeration operation may not execute.</strong> error in my following code.</p>
  
  <pre><code>//temporary var for storing column sort orders according to view type
          Dictionary&lt;string, bool?&gt; tempColumnSortOrders=new Dictionary&lt;string,bool?&gt;(4);
   //Check for column name in col list
          if (tempColumnSortOrders.ContainsKey(fieldToSort))
          {
              //If exists set column sort order to new sort order
              //Set new sort order
              tempColumnSortOrders[fieldToSort] = sortOrder;
              var tempSortOrders = tempColumnSortOrders;
              //remove sort order of other columns
              foreach (var kvp in tempSortOrders)
              {
                  //Reset other columns sort other than current column sort
                  if (kvp.Key != fieldToSort)
                  {
                      tempSortOrders[kvp.Key] = null;
                  }
              }
              //Return name of column to sort
              return fieldToSort;
          }
  </code></pre>
  
  <p><strong>Stack Trace</strong></p>
  
  <blockquote>
    <p><em>[InvalidOperationException: Collection was modified; enumeration operation may not execute.]<br>
    System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource
    resource) +52    System.Collections.Generic.Enumerator.MoveNext() +44 
    GlaziersCenter.Handlers.GetSiteViews.getColumnToSort(Int32 viewType)
    in
    d:\Projects\GlaziersCenter\GlaziersCenter\Handlers\GetSiteViews.ashx.cs:184
    GlaziersCenter.Handlers.GetSiteViews.ProcessRequest(HttpContext
    context) in
    d:\Projects\GlaziersCenter\GlaziersCenter\Handlers\GetSiteViews.ashx.cs:68
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    +341    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +69</em></p>
  </blockquote>