As you are most probably aware, when dealing with collections that will be updated in a WPF application, we tend to prefer using the generic ObservableCollection<T> class. The reason for this is because this class implements the INotifyCollectionChanged interface, which notifies listeners of changes to the collection, such as adding, removing, or clearing items.
What we may not realize is the incredible performance improvement that we get from using this class to hold our data collections. When comparing this with the generic List<T> class, for example, we note that it does not automatically raise any collection changed event. In order to enable the View to display the updated collection, we need to reset it as the ItemsSource property value of the relevant collection control.
However, each time that the ItemsSource property is set, the data bound collection control will clear its current list of items and completely regenerate them again, which can be a time-consuming process. So, to add a single item to an ObservableCollection<T> takes approximately 20 milliseconds to render, but to reset the ItemsSource property value could take over 1.5 seconds.
However, if our collection is immutable and we will not be altering it in any way, we do not need to use the generic ObservableCollection<T> class, as we have no need for its change handlers. Rather than wasting resources on unused change handlers, we can use a different type of collection class.
While there is not a preferred type of collection to use when data binding immutable collections to UI controls, we should try to avoid using the IEnumerable class as the collection container. This type cannot be used directly by the ItemsControl class, and, when it is used, the WPF Framework will generate a generic IList<T> collection to wrap the IEnumerable instance and this can also negatively affect performance.
In the next few sections, we'll explore other ways in which we can display large collections efficiently.