I want iterate a collection, but before I finish to iterate, the collection is modified.
So, whenever the collection is modified, I want to re-iterate the collection from the beginning again.
Any inputs on how will I achieve this?
Printable View
I want iterate a collection, but before I finish to iterate, the collection is modified.
So, whenever the collection is modified, I want to re-iterate the collection from the beginning again.
Any inputs on how will I achieve this?
What kind of collection, specifically? List? Dictionary?
It's a collection from a library. It implemented CollectionBase, which in-turn implemented IList, ICollection, IEnumerable. I've no control over the library as to when the collection will be modified.
Can you clarify how you propose to handle notifications when the collection changes in the middle of your iteration? Are you sending an event that indicatates a change? Does the enumerator return a copy of the collection (or does it return a reference to the actual underlying collection)?
Thank you BioPhyEngr and Arjay.
Let's say the original collection has a, b, c.
a, b, c - auto created
I want to manually create same number of elements as in the collection
d, e, f - manually created
After that, I want to add it to the original collection. In the end, it will be-
a, b, c, d, e, f
The framework will notify me whenver a, b, c is changed. The d, e, f I created is dependent on values of a, b, c. The problem I face is in creation of d, e, f. To create d, e, f I want to iterate the original collection, but while I was iterating, the collection was modified.
In short I want to make sure the original collection will always have same number of manually created elements.
I'll not send any notification. I just want to make sure the collection have same number of my custom created elements.
It's sending the actual underlying collection. But I can use .ToArray() if I need a copy.Quote:
Does the enumerator return a copy of the collection (or does it return a reference to the actual underlying collection)?