In Visual Basic, with the development of the .NET platform, extended collections such as ObservableCollection(Of T) emerged. This collection is designed to notify the user interface (UI) of data changes, which is especially important when working with data binding — for example, in WPF or WinForms with MVVM support. Early constructs like List(Of T) or ArrayList did not support automatic change notifications and required manual logic reworking for display.
Standard collections do not notify the interface or subscribed objects about their own changes — adding, removing, or rearranging elements. As a result, UI controls (such as lists, tables) do not automatically update and require redrawing or additional method calls for updates. This manual synchronization leads to errors and complicates code logic.
ObservableCollection(Of T) implements the INotifyCollectionChanged interface, which allows the UI to automatically learn of any additions, deletions, or changes in elements. For full data updates when the objects in the collection change, it is recommended that the stored elements also implement the INotifyPropertyChanged interface.
Code example:
Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class Item Implements INotifyPropertyChanged Private _name As String Public Property Name As String Get Return _name End Get Set(value As String) If _name <> value Then _name = value RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Name")) End If End Set End Property Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged End Class Dim items As New ObservableCollection(Of Item)() AddHandler items.CollectionChanged, Sub(s, e) Console.WriteLine($"Action: {e.Action}") End Sub items.Add(New Item() With {.Name = "Test 1"}) items(0).Name = "Updated Name"
Key features:
INotifyPropertyChanged is required for automatic synchronization with the UI.Can ObservableCollection notify about changes in properties of elements if the element changes but the collection itself (composition, size) does not change?
No, by default ObservableCollection only tracks changes to the collection itself, not the properties of individual elements. To reflect changes in properties in the UI, objects must implement the INotifyPropertyChanged interface, and the UI binding must support this.
Will notifications about changes occur if the collection is modified outside the UI thread?
No, changes to the collection outside the main (UI) thread will lead to errors or incorrect behavior in some frameworks (e.g., WPF). For multi-threaded changes, you need to use Dispatcher.Invoke or similar methods to ensure synchronization occurs in the correct thread.
Can ObservableCollection be used for binding in WinForms, or does it only work in WPF?
ObservableCollection works in WinForms as well, however, in WinForms manual synchronization and data updating are required, as the standard binding of this platform does not always recognize collection events. In WPF, the implementation is maximally transparent.
ObservableCollection, but the elements within it do not implement INotifyPropertyChanged, hence the UI does not update when properties change.A programmer creates a list of products for the interface, uses a regular List(Of T), and after any addition or removal of items, manually calls update methods for the UI. Sometimes they forget to update the list or incorrectly recalculate indices when removing items.
Pros:
Cons:
ObservableCollection(Of T) is implemented and objects support INotifyPropertyChanged. All actions — addition, deletion, modification — are automatically reflected in the interface through binding. The UI logic becomes more straightforward, with fewer manual updates needed.
Pros:
Cons: