By inheriting the collection <t> type and adding two events itemadded and itemremoved to the collection, you can detect the operations to add and delete items in the container. The event provides data for the element value and index value as follows:
Static void main (string [] ARGs)
{
VaR list = new mycollection <string> ();
List. itemadded + = list_itemadded;
List. itemremoved + = list_itemremoved;
Console. writeline ("list. Add (\" mgen \");");
List. Add ("mgen ");
Console. writeline ("list. Add (\" Zhang \");");
List. Add ("Zhang ");
Console. writeline ("list. insert (0, \" Tony \");");
List. insert (0, "Tony ");
Console. writeline ("list [2] = \" Chen \";");
List [2] = "Chen ";
Console. writeline ("list. Remove (\" Tony \");");
List. Remove ("Tony ");
Console. writeline ("list. Clear ();");
List. Clear ();
}
Static void list_itemremoved (Object sender, collectionitemeventargs <string> E)
{
Console. writeline ("Remove-index value {0} value {1}", E. index, E. item );
}
Static void list_itemadded (Object sender, collectionitemeventargs <string> E)
{
Console. writeline ("Add-index value {0} value {1}", E. index, E. item );
}
Output:
List. Add ("mgen ");
Add-index value 0 value mgen
List. Add ("Zhang ");
Add-index value 1 value Zhang
List. insert (0, "Tony ");
Add-index value 0 value Tony
List [2] = "Chen ";
Remove-index value 2 value Zhang
Add-index value 2 value Chen
List. Remove ("Tony ");
Remove-index value 0 value Tony
List. Clear ();
Remove-index value 1: Chen
Remove-index value 0 value mgen
It is easy to implement. Rewrite the collection <t> type: setitem, insertitem, removeitem, and clearitems methods, and then call the corresponding event to add or delete the operation.
Complete code:
Class collectionitemeventargs <t>: eventargs
{
Public int index {Get; private set ;}
Public t item {Get; private set ;}
Public collectionitemeventargs (INT idx, t item)
{
Index = idx;
Item = item;
}
}
Class mycollection <t>: collection <t>
{
Public event eventhandler <collectionitemeventargs <t> itemadded;
Protected virtual void onitemadded (collectionitemeventargs <t> E)
{
VaR handler = This. itemadded;
If (handler! = NULL)
Handler (this, e );
}
Public event eventhandler <collectionitemeventargs <t> itemremoved;
Protected virtual void onitemremoved (collectionitemeventargs <t> E)
{
VaR handler = This. itemremoved;
If (handler! = NULL)
Handler (this, e );
}
Protected override void clearitems ()
{
For (INT I = count-1; I> = 0; I --)
{
Onitemremoved (New collectionitemeventargs <t> (I, this [I]);
}
Base. clearitems ();
}
Protected override void removeitem (INT index)
{
Onitemremoved (New collectionitemeventargs <t> (index, this [Index]);
Base. removeitem (INDEX );
}
Protected override void insertitem (INT index, t item)
{
Onitemadded (New collectionitemeventargs <t> (index, item ));
Base. insertitem (index, item );
}
Protected override void setitem (INT index, t item)
{
Onitemremoved (New collectionitemeventargs <t> (index, this [Index]);
Onitemadded (New collectionitemeventargs <t> (index, item ));
Base. setitem (index, item );
}
}