Home > .NET > Observable Collections in .NET Framework 4

Observable Collections in .NET Framework 4

Observable Collections in .NET Framework 4

April 30th | 2010

Observable Collections in .NET Framework 4

The observable pattern in software development has been something of interest to many. A former co-worker of mine called it “Hollywood programming: Don’t call us, we’ll call you”. It’s a feature that many Aspect Oriented Programming (AOP) models give, and many have hand-rolled their own.

An observable collection simply allows an object to know when the collection has changed, possibly through an event. This puts the responsibility on the collection to notify all of its listeners for changes, rather than the listeners checking periodically if the collection has changed. If you were to hand-roll your own, you might implement IList<T> on your own, and have your implementation raise an event on itself when Add is called.

Starting in the .NET Framework 4, these have been provided to you in a fairly straight-forward fashion. A new class called ObservableCollection<T> in the namespace System.Collections.ObjectModel does the heavy lifting for you. It contains an event called CollectionChanged that is raised when something changes on your collection. Its delegate contains two items. First is “sender”, which is the collection itself. The second is the “args”, which has a lot of interest to us. It is of type NotifyCollectionChangedEventArgs. The args parameter tells us what happened: how it changed, and what exactly changed. Let’s take a peek at some code to see it.

static void Main(string[] args)
            var collection = new ObservableCollection<string>();
            collection.CollectionChanged += collection_CollectionChanged;
            collection.Add("Hello World!");
            collection.Add("Goodbye World!");
            collection.Remove("Goodbye World!");

        static void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
            switch (e.Action)
                case NotifyCollectionChangedAction.Add:
                    Console.WriteLine("\"{0}\" was added.", e.NewItems[0]);
                case NotifyCollectionChangedAction.Remove:
                    Console.WriteLine("\"{0}\" was removed.", e.OldItems[0]);

The Action on the NotifyCollectionChangedEventArgs tells us if something was removed, added, or moved. NewItems is an array of items that were added, and OldItems contains items that were removed. In the ObservableCollection class, the event will be raised once per-item and the NewItems and OldItems will contain a single item. Other classes that implement the observable pattern in the framework (or possibly your own) may in raise the event once for several items, possibly for both an Add and Remove. One occurrence of this is the observable pattern that is in the AJAX 4.0 Framework when making web service callbacks.

Observable collections are useful when a collection is handed to several actors on the collection, and a single place must know what is occurring on the collection, for example: displaying feedback to the user on a GUI.

Kevin Jones is a Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product. On Twitter? Follow Kevin

  1. Rick
    May 9, 2012 at 1:59 pm

    How do I get rid of your topics column so I can see the code it covers up?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: