Thursday 30 August 2012

List Vs ObservableCollection Vs INotifyPropertyChanged

List
Observable Collection
INotifyPropertyChanged
strongly typed list of objects that can be accessed by index
ObservableCollection is a generic dynamic data collection that provides notifications (using an interface "INotifyCollectionChanged") when items get added, removed, or when the whole collection is refreshed.
INotifyPropertyChanged is not a collection, it’s an interface used in the data object classes to providePropertyChanged notification to clients when any property value gets changed. This will allow you to raisePropertyChanged event whenever the state of the object changes (Added, Removed, and Modified) to the point where you want to notify the underlying collection or container that the state has changed.
Using this you can search, sort, and manipulate lists
Using this you can search, sort, and manipulate lists
INotifyPropertyChanged is compatible on all type of collections like List<T>, ObservableCollection<T>, etc.
To Update the list you need to bind the data source again.
It does not provide any notifications when any property in the collection is changed.
The values in the observable collection are added, removed and changed during runtime in the code behind. The operations (adding and removing an item) in the observable collection will be updated to the UI (Datagrid). But any change in the existing item will not be updated to the UI.



List



ObservableCollection


From the msdn documentation:
It represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
Notice that, like any collection that derives from Collection<T>, its methods accept null parameters and do not throw an exception.
But the main feature of the ObservableCollection<T> are the events it raises when the items it contains change. 

By implementing the INotifyCollectionChanged and INotifyPropertyChanged interfaces, the collection has events for CollectionChanged and PropertyChanged. All these events are related. The first one is raised whenever something changed in the collection, be it Add, Remove, Move, etc. 

This also trigger the PropertyChanged event for the Items[] property. When you’re adding or removing items, PropertyChanged is also raised for the Count property.


INotifyPropertyChanged

 References:

1 comment:

  1. original post is far better than this one.......
    http://www.codeproject.com/Articles/42536/List-vs-ObservableCollection-vs-INotifyPropertyCha

    ReplyDelete