ObservableCollection
Learn how to use the ObservableCollection<T> class to build dynamic lists that automatically notify subscribers when items are added, removed, or modified.
We'll cover the following...
Consider a note management application where adding or deleting entries must trigger an immediate user interface refresh. While a standard List<T> stores data efficiently, it does not inform other parts of the program when its contents change.
A good approach to solve this is using events. The ObservableCollection<T> class is a specialized collection that exposes the CollectionChanged event. We can subscribe to this event to ensure the UI or other logic updates automatically whenever the collection state changes.
Note: The CollectionChanged event also triggers when the entire collection is replaced or when an existing item is swapped for a different one.
Syntax and usage
We instantiate and use ObservableCollection<T> using syntax similar to a standard list. The class resides in the System.Collections.ObjectModel namespace.