Possible Duplicate:
Collection was modified; enumeration operation may not execute - why?
First off, I believe this is NOT a duplicate question, as the context here deals with Extension methods.
I am getting this exception when moving items from one list to another via an Extension method, and I'm a bit confused because the list I'm removing items from is not the list I'm iterating over.
For example, the extension method at a minimum would look like this:
public static void MoveItemsTo(this IList source, IList target, IList items)
{
foreach (var item in items) {
target.Add(item);
source.Remove(item);
}
}
This is method is then called like this:
myCollectionOne.MoveItemsTo(myCollectionTwo, itemsToMove);
I only receive the error if I attempt to remove the item from the source list... This is a bit confusing as the method is iterating over a different list. Something must be going on behind the scenes that I am unaware of...