title: Why can't we change values of a dictionary while enumerating its keys?
body: <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var dictionary = new Dictionary&lt;string, int&gt;()
          {
              {&quot;1&quot;, 1}, {&quot;2&quot;, 2}, {&quot;3&quot;, 3}
          };
  
          foreach (var s in dictionary.Keys)
          {
              // Throws the &quot;Collection was modified exception...&quot; on the next iteration
              // What's up with that?
  
              dictionary[s] = 1;  
          }
      }
  }
  </code></pre>
  <p>I completely understand why this exception is thrown when enumerating a list. It seems reasonable to expect that during enumeration the structure of the enumerated object does not change. However, does changing a <em>value</em> of a dictionary also changes its <em>structure</em>?
  Specifically, the structure of its keys?</p>