. Net (C #): adds a deletion event to a collection member.

Source: Internet
Author: User

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 );

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.