ObservableCollection<T>.CollectionChanged Caveat

DO NOT return null values from collection properties or from methods returning collections. Return an empty collection or an empty array instead.

Users of collection properties often assume that the following code will always work:

IEnumerable<string> list = GetList();
foreach (string name in list)

The general rule is that null and empty (0 item) collections or arrays should be treated the same.

– Framework Design Guidelines, p. 217.

System.Collections.ObjectModel.ObservableCollection<T>‘s event CollectionChanged uses the NotifyCollectionChangedEventArgs event arguments class, which holds two lists: OldValue and NewValue. When either is empty, it will be raised with a null as the list’s reference.

I hope they fix this for the next release.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s