I wonder if it is necessary to check the
NotifyCollectionChangedAction
enum of the NotifyCollectionChangedEventArgs
, when subscribing to the CollectionChanged
event. Every example I stumbled upon does it like this:
myCollection.CollectionChanged += (sender, eventArgs) =>
{
if (eventArgs.Action == NotifyCollectionChangedAction.Add)
{
foreach (SampleClass sampleObject in eventArgs.NewItems)
{
addAction(sampleObject);
}
}
else if (eventArgs.Action == NotifyCollectionChangedAction.Remove)
{
foreach (SampleClass sampleObject in eventArgs.OldItems)
{
removeAction(sampleObject);
}
}
// ...
};
Is it possible to ignore the NotifyCollectionChangedAction
and just simplify the code like this:
myCollection.CollectionChanged += (sender, eventArgs) =>
{
eventArgs.NewItems?.OfType<SampleClass>()
.ToList()
.ForEach(addAction);
eventArgs.OldItems?.OfType<SampleClass>()
.ToList()
.ForEach(removeAction);
};
What are the downsides of this idea? Is there anything I have missed?