C# .NET - The use of List ObservableCollection and INotifyPropertyChanged et al

Asked By Tridip Bhattacharjee on 12-Feb-12 03:32 PM

i many time use List it is a generic dynamic collection but i never use ObservableCollection and INotifyPropertyChanged. so i need to know what is ObservableCollection and INotifyPropertyChanged

how ObservableCollection is different than List i terms of use and also how ObservableCollection is different than INotifyPropertyChanged

please give me small code just to show what is the difference between List ,ObservableCollection and INotifyPropertyChanged

also tell me when one should use ObservableCollection and when INotifyPropertyChanged . please come with sample code. thanks

D Company replied to Tridip Bhattacharjee on 12-Feb-12 12:52 PM
Hello Friend.

Good question.

List is a collection.used to store strongly typed list of objects(here objects means any kind of data) that can be accessed by index. It also provides various built in methods to perform some basic operations like searching , sorting,adding items, editing items and many more. The List<T> class is the generic equivalent of the ArrayList class.

Whereas ObservableCollection is a generic dynamic data collection(here dynamic means run time before binding u can manipulate or reffer it completely) provides notifications when items get deleted,modified,or added.

and INotifyCollectionChanged is an interface which provide all the functionality mentioned above in ObservableCollection


Hope its clear to you now!!
Regards
D
Riley K replied to Tridip Bhattacharjee on 12-Feb-12 08:19 PM


 INotifyPropertyChanged is not a collection, It is an Interface used in classes that provides propertychanged event when 
some value changes

You will mostly use this in Silverlight while doing validations.
Robbe Morris replied to Tridip Bhattacharjee on 12-Feb-12 08:23 PM
An ObservableCollection raises events concerning the structure, size, and altered elements within the collection when something changes (ie a record added to the collection, moved in the collection, or perhaps remove from the collection).  These events are reacted to in XAML (WPF and Silverlight) user interfaces when the collection is bound to a UI control or element of a control.
[)ia6l0 iii replied to Tridip Bhattacharjee on 12-Feb-12 10:18 PM
I hope Rob's answer provides you a more insight into the powerfulness of the ObservableCollection. The others are rather pure definitions from MSDN. 

But also note that, ObservationCollection is not thread-safe and is also less flexible than a http://msdn.microsoft.com/en-us/library/ms132679.aspx at times. You have to determine their usage on a case-by-case basis.

And for examples - look at this http://www.codeproject.com/Articles/42536/List-vs-ObservableCollection-vs-INotifyPropertyCha. It is pretty descent. 

Thank you.



Somesh Yadav replied to Tridip Bhattacharjee on 13-Feb-12 12:17 AM
Hi,
http://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each/851129#851129 I think AddRange is better implemented like so:
 public void AddRange(IEnumerable<T> collection)
    {
       foreach (var i in collection) Items.Add(i); OnCollectionChanged(new      NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
       }
 It saves you a list copy. Also if you want to micro-optimise you could do adds for ..

The datagrid would be bound to a list of objects. If you want the grid to update when individual object properties change, than each contained object must implement the INotifyPropertyChanged interface. The INotifyCollectionChanged is an interface that the collection should implement, and are for notifications of addition and removal events.
Tridip Bhattacharjee replied to D Company on 13-Feb-12 02:04 AM
thanks
Tridip Bhattacharjee replied to D Company on 13-Feb-12 02:06 AM
u said "ObservableCollection is a generic dynamic data collection(here dynamic means run time before binding u can manipulate or reffer it completely) " would plzz discuss this point in detail. why it is called dynamic data collection

LIST is not dynamic data collection...if not then why.