I have a collection with thousands of nodes. I’d like each node to fire a Changed() event to notify the container.
Normally, registering an event creates a new EventHandler<>:
Node node = new Node();
node.Changed += new EventHandler<EventArgs>(OnChanged);
This would result in thousands of EventHandler<> objects.
I’d like to know if it is possible to construct a single EventHandler<> and use it simultaneously with thousands of nodes:
class Container {
EventHandler<EventArgs> eventHandler =
new EventHandler<EventArgs>(OnChanged);
void CreateNode() {
Node node = new Node();
node.Changed += eventHandler;
}
}
Yes, you can: Reference the
System.Collections.ObjectModel.ObservableCollection<T>as described on MSDN: http://msdn.microsoft.com/en-us/library/ms668604.aspxHere is a sample class that notifies when it’s properties are changed:
Then use the
System.Collections.ObjectModel.ObservableCollection<T>because it fires the following events: