• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: System.InvalidOperationException: ‘Collection was modified; enumeration operation may not execute.’

Resolved: System.InvalidOperationException: ‘Collection was modified; enumeration operation may not execute.’

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m trying to implement https://docs.microsoft.com/en-us/dotnet/standard/events/observer-design-pattern into my pet shop for a school project. However I keep running into this error. I already tried looking it up but most common solutions like .ToList(); or ToArray(); dont work.
My observer class:
This part doens’t work:
Display class:

Answer:

Move the loop over animalsToRemove outside the foreach loop over animals:

var animalsToRemove = new List();
foreach(var animal in animals) <--- Error point { if(info.AnimalId == animal.AnimalId) { animalsToRemove.Add(animal); foreach (var observer in observers) { observer.OnNext(info); } } } foreach (var animalToRemove in animalsToRemove) { animals.Remove(animalToRemove); } animalsToRemove.Clear(); [/code]

If you have better answer, please add a comment about this, thank you!

c# console-application
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: what are the permissions required to assign a resource monitor to a warehouse?

01/04/2023

Resolved: How can I modify formData before sending it?

01/04/2023

Resolved: How to efficient create SimpleITK image?

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.