title: Looping Over Dictionary in C#
body: <p>I realize that you cannot iterate over a Dictionary in C# and edit the underlying Dictionary as in the following example:</p>
  
  <pre><code>Dictionary&lt;Resource, double&gt; totalCost = new Dictionary&lt;Resource, double&gt;();
  // Populate the Dictionary in here - (not showing code).    
  foreach (Resource resource in totalCost.Keys)
  {
       totalCost[resource] = 5;
  }
  </code></pre>
  
  <p>One way I see to fix this is to make a List backed by the Dictionary's keys, like this:</p>
  
  <pre><code>Dictionary&lt;Resource, double&gt; totalCost = new Dictionary&lt;Resource, double&gt;();
  // Populate the Dictionary in here - (not showing code).    
  foreach (Resource resource in new List(totalCost.Keys))
  {
       totalCost[resource] = 5;
  }
  </code></pre>
  
  <p>Because I'm not editing the keys themselves, is there any reason that this should not be done or that it's bad to choose this as a solution.  (I realize if I was editing those keys, this could cause a lot of problems.)</p>
  
  <p>Thank you.</p>
  
  <p><strong>Edit:</strong> Fixed my code example.  Sorry about that.</p>