Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

List vs ObservableCollection vs INotifyPropertyChanged in Silverlight

,
Rate me:
Please Sign up or sign in to vote.
4.60/5 (90 votes)
21 Sep 2009CC (ASA 2.5)2 min read 366.2K   5.7K   110   46
This article gives a basic understanding of List, ObservableCollection, and INotifyPropertyChanged.

Introduction

This article describes the basic understanding between List, ObservableCollection and INotifyPropertyChanged.

Difference between List<T>, ObservableCollection<T> and INotifyPropertyChanged

List<T>

It represents a strongly typed list of objects that can be accessed by index. It provides methods to search, sort, and manipulate lists. The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface using an array whose size is dynamically increased as required.

Image 1

Drawbacks

In ASP.NET, we simply use DataSource and DataBind() to bind the data, but in Silverlight it is slightly different. Databinding in ASP.NET is done in a stateless way - once that binding operation is completed, it's a done deal and if you want to change anything, you have to manipulate the underlying controls that were created as a result of the data binding, or else change the underlying data objects and call DataBind() again. That’s what we are used to – but it’s not a good practice.

listgrid.JPG

In the sample application, the values in the list are added, removed and changed during runtime in the code behind. The changes in the list will not be updated to the UI (Datagrid).

ObservableCollection<T>

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.

Note: WCF service proxy class in Silverlight will use this type of collection by default.

observablecollection.JPG

Drawbacks

It does not provide any notifications when any property in the collection is changed.

observablecollectiongrid.JPG

In the sample application, 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.

INotifyPropertyChanged

INotifyPropertyChanged is not a collection, it’s an interface used in the data object classes to provide PropertyChanged notification to clients when any property value gets changed. This will allow you to raise PropertyChanged 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.

inotifypropertychanged.JPG

inotifypropertychangedgrid.JPG

INotifyPropertyChanged is compatible on all type of collections like List<T>, ObservableCollection<T>, etc. The code snippet which uses INotifyPropertyChanged is shown below:

C#
public class UserNPC:INotifyPropertyChanged
{
    private string name;
    public string Name { 
        get { return name; } 
        set { name = value; onPropertyChanged(this, "Name"); } 
    }
    public int grade;
    public int Grade { 
        get { return grade; } 
        set { grade = value; onPropertyChanged(this, "Grade"); } 
    }

    // Declare the PropertyChanged event
    public event PropertyChangedEventHandler PropertyChanged;

    // OnPropertyChanged will raise the PropertyChanged event passing the
    // source property that is being updated.
    private void onPropertyChanged(object sender, string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
        }
    }
}

In the above code snippet, whenever a value is set to a property, the method “onPropertyChanged” will be called which in turn raises the PropertyChanged event.

History

  • 22nd September, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Technical Lead Infosys
India India
Working as a Technology Lead in Infosys at Chennai, India.

Written By
Software Developer iSOFT
India India
Working as a Software Engineer in iSOFT at Chennai, India.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Sacha Barber30-Mar-11 0:32
Sacha Barber30-Mar-11 0:32 
GeneralMy vote of 5 Pin
Sandeep Mewara21-Dec-10 22:51
mveSandeep Mewara21-Dec-10 22:51 
GeneralCopy the EventHandler into a local variable before using it Pin
alrabe5-Nov-10 13:09
alrabe5-Nov-10 13:09 
GeneralMy vote of 4 Pin
pacreau18-Oct-10 3:51
pacreau18-Oct-10 3:51 
GeneralMy vote of 4 Pin
i-jomalo21-Sep-10 3:13
i-jomalo21-Sep-10 3:13 
GeneralMy vote of 5 Pin
parthkotak19-Sep-10 22:36
parthkotak19-Sep-10 22:36 
GeneralMy vote of 5 Pin
tadanderson13-Sep-10 6:32
tadanderson13-Sep-10 6:32 
GeneralMy vote of 5 Pin
abhix13-Aug-10 0:30
abhix13-Aug-10 0:30 
simple & visual explaination
GeneralThanks Pin
AG_P111-Aug-10 15:26
AG_P111-Aug-10 15:26 
GeneralMy vote of 3 Pin
vijayakumar_sht15-Jul-10 1:12
vijayakumar_sht15-Jul-10 1:12 
GeneralGood Job guys Pin
Supply Chain7-Jun-10 18:58
Supply Chain7-Jun-10 18:58 
GeneralGood article Pin
vikas amin12-Feb-10 4:23
vikas amin12-Feb-10 4:23 
GeneralObservableCollection + INotifyPropertyChanged PinPopular
Alex Mikunov20-Nov-09 20:41
Alex Mikunov20-Nov-09 20:41 
GeneralDoes the job Pin
blackjack215022-Sep-09 2:43
blackjack215022-Sep-09 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.