Hi
I'm struggling with this loop. It gives me "Collection was modified; enumeration operation may not execute."
I think it is about changing the CarsList. The RentIn method adds the car object to CarsList. So it modifies the list in the foreach loop. If I am correct, how could I use these methods? Since they modify the list on the run. I don't see any other way to do it, without foreach.
Code Code Code Code Code Code Code Code Code |
Thank you

Manikavelu VelayuthamRank #413 · 6K reputationPosted Oct 14, 2010, 12:41 AM EDT
There were several ways to fix this issue.
First try this one, hope this alone will fix the issue, if not let me know
replace the code from
foreach (CarRental carRental in CarRentalProgram.CarRentalsList)
to
foreach (CarRental carRental in CarRentalProgram.CarRentalsList.ToList())