Custom (derived) List<T>

Viewed 183

Feel free to load your guns and take aim, but I want to understand why you shouldn't do this.

I have created a custom class designed to replace any instances of List (which I use to update XML objects behind them):

public class ListwAddRemove<T> : List<T> {
    public event EventHandler<ListModifyEventArgs> OnAdd;
    public event EventHandler<ListModifyEventArgs> OnRemove;

    new public void Add(T o) {
        base.Add(o);

        if (OnAdd != null) {
            OnAdd(this, new ListModifyEventArgs(o));
        }
    }

    new public void Remove(T o) {
        base.Remove(o);

        if (OnRemove != null) {
            OnRemove(this, new ListModifyEventArgs(o));
        }
    }
}

The idea is whenever I add or remove an item from this list my bound events will fire and I can deal with the XML behind automatically.

This works like a charm, so far so good.

But how do I handle a conversion between object.ToList() and my derived version?

A lot of people are saying you should derive from Collection instead... why?

2 Answers
Related