Search code examples
c#observablecollectioneventargsinotifycollectionchanged

Why there is list of changes in observable collection event args?


I'm trying to understand why there are two list properties NewItems and OldItems while you can only add or remove single item at a time?

private void InternalCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
    var changes = args.NewItems; // why this is a list?
}

I looked at available methods for ObservableCollection and I can only add or remove single item at a time.

if I can only add or remove single item at a time, what is the reason to have list of changes in event args and not a single add or remove change?


Solution

  • ObservableCollection<T> is an implementation of INotifyCollectionChanged inteface and indeed it does not support adding or removing several items. But you can create your own collection class implementing INotifyCollectionChanged (and INotifyPropertyChanged) which will support adding or removing multiple items.