If you try to enumerate through a Dictionary's key collection and attempt to change the values as in the code below,
Dictionary<string, string> _items = new Dictionary<string, string>();
...
int i = 0;
foreach (string key in _items.Keys)
{
_items[key] = elements[i++];
}
you will get the following error:
System.InvalidOperationException was unhandled
Message="Collection was modified; enumeration operation may not execute."
You would think that if you are not changing the keys, then you will not foul up the enumerator, but you'd be wrong.
The solution is to copy out the key collection and iterate through that.
int i = 0;
string [] keys = _items.Keys.ToArray<string>();
for (i = 0; i < keys.Length; i++)
{
_items[keys[i]] = elements[i];
}