I need a .net Collection that has Add and Remove events. Is there a premade version that has that?
For example, I would update some internal counter when a user adds an item to the list via the event. (Not really what I plan to do.)
I need a .net Collection that has Add and Remove events. Is there a premade version that has that?
For example, I would update some internal counter when a user adds an item to the list via the event. (Not really what I plan to do.)
Try ObservableCollection<T>
it supports INotifyCollectionChanged
which supplies events for every aspect of collection modification.
Namespace: System.Collections.ObjectModel
Assembly: WindowsBase.dll
ObservableCollection<T> is as close as you'll get in the framework.
OTOH, it isn't that hard to do this. Just create a class that implements IList and wraps an internal List<T> instance. You can just throw your events as needed.
You can also write a custom lightweight wrapper if you wish to optimize for speed at the expense of flexibility.
Not that i know of but this would be pretty easy to implement, you can just create your own add and remove methods, or you could use an indexer. Then you can do whatever you like in terms of events.
You could always make a private List<...>
and then access through a method:
private List<string> listOfStrings = new List<string>;
public void AddToList(string s)
{
// Do your work
listOfStrings.Add(s);
}
or you could overload it and add events however you wish.