Events in Generic List

In a C# program I've been working on, I have a class that makes use of a couple of generic lists to store some data. When the form that displays the database is closed, I do a check to see if the class is dirty. I've got some properties set up for the singular values so that when they changed, the dirty state gets updated, but I haven't got the lists updating the dirty flag yet. The way that I'm looking at doing it is attaching an event handler the the add, remove, and so on. In the non-generic CollectionBase, these events are exposed, but not so for the generic List class I'm using. I'm looking at creating a new list class that inherits from list and implements these events, but before I do, I just want to make sure that I'm not missing something. Is there any way to bind to the base generics' events without creating a new class and doing my own


Answer this question

Events in Generic List

  • manticor32427

    You may also want to try the System.ComponentModel.BindingList<t>. That class has events built in for every thing. Also if you're using Windows Forms databinding Adds, Removes and Updates ( updates only if your list items implement the System.ComponentModel.INotifyPropertyChanged interface) will update the UI automagically. The downer is that the BindingList does not support all the cool predicates or actions that the List<t> supports.

  • Basils

    No, your analysis is correct. There are no overridable methods in List<T> that you can use. The non-generic and generic collections (both the interfaces and base classes) are dramatically different. Even the very definition of a collection changes between non-generic and generic methods. Therefore if you want to have events in a generic list you'll need to create your own collection class. Note that you don't actually have to implement all the logic (just embed a List<T> in your custom list class) but you do have to expose the interface and any additional methods you want.

    As a side note the primary difference between a list and collection in v1.x was that one was editable and the other wasn't. This has changed in v2.0 where a collection is now editable. Therefore you might do better just to switch to Collection<T> instead of using List<T>. More importantly however is that it does have overridable methods that you can use to raise events. Unfortunately it is pretty limited beyond that so you'll find yourself writing a new collection class if you want much more functionality than that. Thus, in a nutshell, I'd try using Collection<T> instead.

    Michael Taylor - 3/1/06


  • sweetcheeks

    Thanks Michael,

    I looked into the Collection's events, and I would have had to change a few things in my program to use it, and would have had to rewrite the events anyway to get what I wanted, so I just went ahead and inherited from List<T> and added a change event that I tied to add and remove. Everything looks good, I just had to make sure that some of the functions that returned List<T> are returning my new class, because it didn't like that.

  • Events in Generic List