-1

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?

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • https://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each – BugFinder Feb 02 '18 at 09:00

2 Answers2

2

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.

Pavel
  • 910
  • 5
  • 6
  • oh, I see. thanks for the answer. single change at a time sounds easier to manage. so basically I can write `NewItems[0]` instead of doing foreach. I was worried maybe I'm wrong – M.kazem Akhgary Feb 02 '18 at 08:54
1

As an additional info to @Pavel's answer, ObservableCollection<T> does not support this out of the box, but you can create custom derived type of the ObservableCollection<T> that supports this. A great example is ObservableRangeCollection from James Montemagno or a even more optimized version in this Stack Overflow answer.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91