C # basic delegate usage

Source: Internet
Author: User

Due to the new access to C #, I have also read some of the C # DelegationArticleBut I have never understood it thoroughly. Now I will write down my rough understanding and hope you can give me some advice.

When the parent class wants to call the subclass method, you can consider using delegation: because the parent class cannot call the methods in the subclass, you can call the subclass method by defining the delegate. The procedure is as follows:

1) define a delegate; Public Delegate void openfileeventhandler (Object sender, openfileeventargs E );

2) define an event in the parent class; public event openfileeventhandler openfile;

3) register the event where the subclass instantiates the parent class; openfile + = new openfileeventhandler (openfile); the signature of the openfile () method must be consistent with that of the delegate.

4) call this event where the parent class needs to call the subclass method; If (openfile! = NULL) openfile (sender, e );

In this way, when the parent class runs to openfile (sender, e);, the openfile (); Method in the subclass is called.

I hope you can give us some guidance.

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.