Avoid Collection has been modified error?

You mentioned IDisposable which offers one way you could implement this: public class GenericList : IList { private class CleanupEnumerator : IEnumerator { private readonly GenericList source; public CleanupEnumerator(GenericList source) { this. Source = source; } public void Dispose() { source. RemovePendingDeletes(); } /* Other IEnumerator methods here */ } public IEnumerator GetEnumerator() { return new CleanupEnumerator(this); } /* Other IList methods here */ } This will guarantee that any time your collection is enumerated using foreach the RemovePendingDeletes() function will get called when the enumerator is disposed.Be warned, however, that this could get ugly if you ever call GetEnumerator() directly and forget to dispose the enumerator.

You mentioned IDisposable, which offers one way you could implement this: public class GenericList : IList { private class CleanupEnumerator : IEnumerator { private readonly GenericList source; public CleanupEnumerator(GenericList source) { this. Source = source; } public void Dispose() { source. RemovePendingDeletes(); } /* Other IEnumerator methods here */ } public IEnumerator GetEnumerator() { return new CleanupEnumerator(this); } /* Other IList methods here */ } This will guarantee that any time your collection is enumerated using foreach, the RemovePendingDeletes() function will get called when the enumerator is disposed.Be warned, however, that this could get ugly if you ever call GetEnumerator() directly and forget to dispose the enumerator.

This looks like what i'm after. Would the Dispose method definately get called as soon as control leaves the forloop block (whether it be a break or a normal exit? – George Duckett Jun 10 at 12:56 @George, foreach will guarantee that the Dispose method gets called.

Foreach is basically syntactic sugar for using (var e = o.GetEnumerator()) { while (e.MoveNext()) { ... } }. – JSBᾶngs Jun 10 at 13:00 But even with this solution the pending deletes need to be stored somewhere. This somewhere will probably be some kind of collection that will consume memory and produce GC pressure.In my opinion this does not fulfill the OPs requirement of "I want to avoid any unnecessary copying as I want to eliminate/minimise garbage collection so I can't just iterate over a copy/make a new copy or anything like that.

" – Florian Greinacher Jun 10 at 13:10 Nevertheless without this requirement the solution would be a pretty elegant solution to the problem! – Florian Greinacher Jun 10 at 13:10 @JSBangs, thanks for the clarification. – George Duckett Jun 10 at 13:21.

Why you want to have one foreach loop and store somewhere what needs to be removed and remove it using some other looping mechanism. Instead of this you can simply use a for loop in reverse like following code snippet. For (int I = GenericListInstanceB.

Length - 1; I >= 0; i--) { var ItemA = GenericListInstanceBi; ItemA. MethodThatCouldRemoveAnyItemInGenericListInstanceB(); }.

MethodThatCouldRemoveAnyItemInGenericListInstanceB could remove an item at index before i. Which would mean the item would get processed twice. Adding would cause an item to not get processed if it was inserted before I – George Duckett Jun 10 at 11:04 1 This will result in an item being evaluated multiple times if things before it are removed – Robert Levy Jun 10 at 11:04.

How about this. I'm assuming the MethodThatCouldRemoveAnyItemInGenericListInstanceB function removes one or no items from anywhere in the list, no items will be added. Bool finished = false; int I = 0; while (!finished) { var itemA = genericListInstanceBi; itemA.

MethodThatCouldRemoveAnyItemInGenericListInstanceB(); if (genericListInstanceBi == itemA) i++; // All other outcomes result in us leaving I alone finished = (i > (genericListInstanceB. Count - 1)); } Dangerous, old fashioned and "smelly" but, it does what you want without having to have a specialised list or some magic to trap the disposal of the list.

Thanks for all the effort, but I think there's still corder cases (maybe I should ahve been more clear). Things like if one item as added and one is removed, or if 2 items are added etc. – George Duckett Jun 10 at 12:54 Yep, this obviously won't work for that. The only idea I have in that scenario is a iterative function that works out where to go next.

This problem can't be solved without some extra state. Whether that is internal to a specialised list, or perhaps a hashtable to detect changes between operations.Do the additions have to be processed too? – Jodrell Jun 10 at 13:21.

Anyway, avoid lists of a milion items. You should work with paged lists of objects.

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