In C#, the ObservableCollection<T> collection class plays a fundamental role in this architecture.
The ObservableCollection<T> represents a dynamic data collection that can provide a notification when the collection is modified. This includes adding, removing, or refreshing the list.
This makes it useful in applications where you need to update the application when the data change occurs. An example is when dealing with UWP, WPF, Xamarin applications, etc.
In this tutorial, we will explore the fundamentals of working with the ObservableCollection to create and use the dynamic data binding.
Add Support for ObservableCollection
Before we can use the ObservableCollection in a project, we need to include the namespace in our application. We can do this using the “using” keyword as follows:
Once imported, we can proceed to create and use this collection.
Create an ObservableCollection
The following shows the basics of creating an ObservableCollection that contains the string values:
{
"start",
"stop 2",
"close"
};
Add Items
To add items to the collection, we can use the “Add” method as follows:
This should add a new item and reflect the changes to the receiving entity.
Remove the Items
To remove the items from a collection, we use the Remove() method as follows:
This should remove the specified element from the collection.
Conclusion
This is a basic article that introduces you to dynamic data binding using the ObservableCollection class.