BindingList<T> : An Eventful Collection

The other day, I discovered the Generic collection, BindingList, in the System.ComponentModel namespace after reading about it in Rocky Lhotka's recently revised and updated Expert C# Business Objects.

I had recently ran into the need to have a business object that had a couple of generic collections as members and when those collections changed, I needed for other properties to be updated within this business object. Not aware of the existence of BindingList, I subclassed Collection for the type I was interested in, added an event and called that event for all of the overrides that could be used to change the collection. I then could subscribe to this event within my business object to do what I needed to do to update the calculated properties.

BindingList serves this purpose exactly and more. It is designed to provide great DataBinding features, however, it had this exact eventing structure that I just wrote by hand already prewired in the BindingList.ListChanged event.

Tags: software, development, bindinglist, collection, generics, .net 2.0, rocky lhotka, expert business objects