When Clearing an ObservableCollection, There are No Items in e.OldItems

It doesn’t claim to include the old items, because Reset doesn’t mean that the list has been cleared

It means that some dramatic thing has taken place, and the cost of working out the add/removes would most likely exceed the cost of just re-scanning the list from scratch… so that’s what you should do.

MSDN suggests an example of the entire collection being re-sorted as a candidate for reset.

To reiterate. Reset doesn’t mean clear, it means Your assumptions about the list are now invalid. Treat it as if it’s an entirely new list. Clear happens to be one instance of this, but there could well be others.

Some examples:
I’ve had a list like this with a lot of items in it, and it has been databound to a WPF ListView to display on-screen.
If you clear the list and raise the .Reset event, the performance is pretty much instant, but if you instead raise many individual .Remove events, the performance is terrible, as WPF removes the items one by one.
I’ve also used .Reset in my own code to indicate that the list has been re-sorted, rather than issuing thousands of individual Move operations. As with Clear, there is a large performance hit when when raising many individual events.

Leave a Comment