C# Collection was modified; enumeration operation may not execute?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

Any collection that you iterate over with foreach may not be modified during iteration.

Any collection that you iterate over with foreach may not be modified during iteration. So while you're running a foreach over rankings, you cannot modify its elements, add new ones or delete any.

The error tells you EXACTLY what the problem is (and running in the debugger or reading the stack trace will tell you exactly where the problem is): C# Collection was modified; enumeration operation may not execute. Your problem is the loop foreach (KeyValuePair kvp in rankings) { // } wherein you modify the collection rankings. In particular, the offensive line is rankingskvp.

Key = rankingskvp. Key + 4; Before you enter the loop, add the following line: var listOfRankingsToModify = new List(); Replace the offending line with listOfRankingsToModify. Add(kvp.

Key); and after you exit the loop foreach(var key in listOfRankingsToModify) { rankingskey = rankingskey + 4; } That is, record what changes you need to make, and make them without iterating over the collection that you need to modify.

I suspect the error is caused by this: foreach (KeyValuePair kvp in rankings) rankings is a dictionary, which is IEnumerable. By using it in a foreach loop, you're specifying that you want each KeyValuePair from the dictionary in a deferred manner. That is, the next KeyValuePair is not returned until your loop iterates again.

But you're modifying the dictionary inside your loop: rankingskvp. Key = rankingskvp. Key + 4; which isn't allowed...so you get the exception.

You could simply do this foreach (KeyValuePair kvp in rankings.ToArray()).

The problem is where you are executing: rankingskvp. Key = rankingskvp. Key + 4; You cannot modify the collection you are iterating through in a foreach loop.

A foreach loop requires the loop to be immutable during iteration. Instead, use a standard 'for' loop or create a new loop that is a copy and iterate through that while updating your original.

As others have pointed out, you are modifying a collection that you are iterating over and that's what's causing the error. The offending code is below: foreach (KeyValuePair kvp in rankings) { ..... if((double)(similarModules/modules. Count)>0.6) { rankingskvp.

Key = rankingskvp. Key + 4; // e = ((IEnumerable)values).GetEnumerator(); // = null) ((IDisposable)e).Dispose(); } } If you look up the MSDN documentation for IEnumerable (which is what GetEnumerator() returns) you will see: Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection. Which brings us back to what the error message states and the other answers re-state, you're modifying the underlying collection.

Potential readers: Please ignore the downvote, it was done in a fit of nerdrage. ROMAN: if you edit this answer I'll remove the downvote, its past the time limit and won't let me do it. – kekekela Jun 22 at 21:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions