title: C# - Change value of dictionary key-value pair while in foreach
body: <p>I have this code which is being run every frame of a game:</p>
  
  <pre><code>    foreach (var repeaterAction in conditionTimes.Keys)
      {
          if (repeaterAction.Condition() == true)
          {
              if (conditionTimes[repeaterAction] == TimeSpan.Zero)
              {
                  repeaterAction.Action();
              }
              else if (conditionTimes[repeaterAction] &gt;= repeaterAction.InitialLapse)
              {
                  repeaterAction.Action();
                  conditionTimes[repeaterAction] -= repeaterAction.ActionInterval;
              }
              conditionTimes[repeaterAction] += gameTime.ElapsedGameTime;
          }
          else
          {
              conditionTimes[repeaterAction] = TimeSpan.Zero;
          }
      }
  </code></pre>
  
  <p>This is giving me the following error:</p>
  
  <blockquote>
    <p>Collection was modified; enumeration operation may not execute.</p>
  </blockquote>
  
  <p>Is there a way to modify the value in the key-value pair inside the foreach loop, without copying the Dictionary every frame?</p>