I want to know the difference between ObservableCollection
and BindingList
because I've used both to notify for any add/delete change in Source, but I actually do not know when to prefer one over the other.
Why would I choose one of the following over the other?
ObservableCollection<Employee> lstEmp = new ObservableCollection<Employee>();
or
BindingList<Employee> lstEmp = new BindingList<Employee>();
Best Answer
An
ObservableCollection
can be updated from UI exactly like any collection. The true difference is rather straightforward:ObservableCollection<T>
implementsINotifyCollectionChanged
which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when theObservableCollection
is updated.However,
BindingList<T>
implementsIBindingList
.IBindingList
provides notification on collection changes, but not only that. It provides a whole bunch of functionality which can be used by the UI to provide a lot more things than only UI updates according to changes, like:All these functionalities are not available in
ObservableCollection<T>
Another difference is that
BindingList
relays item change notifications when its items implementINotifyPropertyChanged
. If an item raises aPropertyChanged
event, theBindingList
will receive it an raises aListChangedEvent
withListChangedType.ItemChanged
andOldIndex=NewIndex
(if an item was replaced,OldIndex=-1
).ObservableCollection
doesn't relay item notifications.Note that in Silverlight,
BindingList
is not available as an option: You can however useObservableCollection
s andICollectionView
(andIPagedCollectionView
if I remember well).