You cant change the content of an enumeration while enumerating the enumeration, foreach does enumerate an enumeration (nice). The reason is, if you change the content the enumerator which is used by foreach will be invalid.
You can fix this by creating an isolated copy of your enumeration and iterate over these:
Yeah but both of your ways give the same out of sync error.
Anyways, i ended up making up a new dictionary and copying stuff back and forth. And it works for the case.
Oh, I see that the second example is indeeed faulted, but the first one should never give an out of sync error as it works on a completely different enumeration and does never change this enumeration.