Explanation
There is a type of Collection in C# that notifies changes made to the list through events, the Observablecollection.
With it, you can assign a method to the event CollectionChanged
to receive change notifications in the list and also use the event PropertyChanged
to receive notifications of changes to properties.
Example
using System;
using System.Collections.ObjectModel;
namespace ObservableCollectionExample
{
class Program
{
static void Main(string[] args)
{
ObservableCollection<string> list = new ObservableCollection<string>();
list.CollectionChanged += List_CollectionChanged;
list.Add("Teste 1");
list.Add("Teste 2");
}
private static void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Console.WriteLine("A lista foi alterada.");
}
}
}
See working on Dotnetfiddle
But what if that list is not stated in the main ?
– Amadeu Antunes
This was just an example. You can declare where you want, as long as the reference is valid.
– William John Adam Trindade