ObservableCollection

Learn about a collection with a built-in notification mechanism.

We'll cover the following

Overview

Imagine we’re building an application to manage notes. We can add and delete notes. We want the user interface to update itself whenever an item is added or deleted. How do we know when this occurs?

A good approach is to use events. The ObservableCollection<T> is a collection just like List<T>, but it exposes the CollectionChanged event that is triggered whenever an item is added to or removed from the list.

Note: The CollectionChanged event is also triggered when the whole collection or an item is replaced with a different one.

We can subscribe to this event and make the UI update itself.

Syntax and usage

We instantiate and use ObservableCollection<T> just like a list. The class resides in the System.Collections.ObjectModel namespace. After importing it, we can start using this class in our programs:

Get hands-on with 1200+ tech skills courses.