If you need to extend an existing collection type you should use Collection<T>
which is designed for this purpose. For example:
public class BetList : Collection<Bet>
{
public uint Sum { get; private set; }
protected override void ClearItems()
{
Sum = 0;
base.ClearItems();
}
protected override void InsertItem(int index, Bet item)
{
Sum += item.Amount;
base.InsertItem(index, item);
}
protected override void RemoveItem(int index)
{
Sum -= item.Amount;
base.RemoveItem(index);
}
protected override void SetItem(int index, Bet item)
{
Sum -= this[i].Amount;
Sum += item.Amount;
base.SetItem(index, item);
}
}
A good explanation of the differences between List<T>
and Collection<T>
can be found here: What is the difference between List (of T) and Collection(of T)?
The above class would be used like this:
var list = new BetList();
list.Add( bet ); // this will cause InsertItem to be called