Click here to Skip to main content
Licence CC (ASA 2.5)
First Posted 22 Sep 2009
Views 89,071
Downloads 1,775
Bookmarked 60 times

List vs ObservableCollection vs INotifyPropertyChanged in Silverlight

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.

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:

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

About the Authors

BalaG Ganesan

Software Developer (Senior)
iSOFT
India India

Member

Working as a Senior Software Engineer in iSOFT at Chennai, India.

Venkatesan Jagadisan

Software Developer
iSOFT
India India

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmembergovardhanpagidi20:26 29 Apr '12  
GeneralMy vote of 5 Pinmemberpiapp0:14 24 Apr '12  
GeneralGreat PinmemberMember 37107031:51 18 Apr '12  
QuestionIt's very good article. PinmemberAshokReddyProjects1:23 19 Jan '12  
GeneralMy vote of 5 Pinmemberm@dhu2:51 27 Dec '11  
QuestionExcellent Contrast PinmemberMember 15408997:00 3 Nov '11  
QuestionNice article PinmemberSaru.cool21:04 5 Sep '11  
GeneralGreat explanation: Vote of 5 PinmemberJaved Akhtar Ansari20:27 16 Aug '11  
QuestionCorrections... PinmemberMember 477740523:56 13 Jul '11  
GeneralMy vote of 5 PinmemberAzhar Saiyed19:04 4 Jul '11  
GeneralMy vote of 4 Pinmemberrushijoshi0:53 25 May '11  
GeneralMy vote of 2 PinmvpSacha Barber0:32 30 Mar '11  
GeneralMy vote of 5 PinmentorSandeep Mewara22:51 21 Dec '10  
GeneralCopy the EventHandler into a local variable before using it Pinmemberalrabe13:09 5 Nov '10  
GeneralMy vote of 4 Pinmemberpacreau3:51 18 Oct '10  
GeneralMy vote of 4 Pinmemberi-jomalo3:13 21 Sep '10  
GeneralMy vote of 5 Pinmemberparthkotak22:36 19 Sep '10  
GeneralMy vote of 5 Pinmembertadanderson6:32 13 Sep '10  
GeneralMy vote of 5 Pinmemberabhix0:30 13 Aug '10  
GeneralThanks PinmemberAG_P115:26 11 Aug '10  
GeneralMy vote of 3 Pinmembervijayakumar_sht1:12 15 Jul '10  
GeneralGood Job guys PinmemberSupply Chain18:58 7 Jun '10  
GeneralGood article Pinmembervikas amin4:23 12 Feb '10  
GeneralObservableCollection + INotifyPropertyChanged PinPopularmemberAlex Mikunov20:41 20 Nov '09  
GeneralDoes the job Pinmemberblackjack21502:43 22 Sep '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 22 Sep 2009
Article Copyright 2009 by BalaG Ganesan, Venkatesan Jagadisan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid