Can't add/remove items from a collection while foreach is iterating over it

前端 未结 4 1232
旧时难觅i
旧时难觅i 2020-12-10 19:21

If I make my own implementation of IEnumerator interface, then I am able ( inside foreach statement )to add or remove items from a albumsList

4条回答
  •  眼角桃花
    2020-12-10 20:01

    From the MSDN documentation for INotifyCollectionChanged:

    You can enumerate over any collection that implements the IEnumerable interface. However, to set up dynamic bindings so that insertions or deletions in the collection update the UI automatically, the collection must implement the INotifyCollectionChanged interface. This interface exposes the CollectionChanged event that must be raised whenever the underlying collection changes.

    WPF provides the ObservableCollection<(Of <(T>)>) class, which is a built-in implementation of a data collection that exposes the INotifyCollectionChanged interface. For an example, see How to: Create and Bind to an ObservableCollection.

    The individual data objects within the collection must satisfy the requirements described in the Binding Sources Overview.

    Before implementing your own collection, consider using ObservableCollection<(Of <(T>)>) or one of the existing collection classes, such as List<(Of <(T>)>), Collection<(Of <(T>)>), and BindingList<(Of <(T>)>), among many others.

    If you have an advanced scenario and want to implement your own collection, consider using IList, which provides a non-generic collection of objects that can be individually accessed by index and provides the best performance.

    Sounds to me that the problem is in the Collection itself, and not its Enumerator.

提交回复
热议问题