C # Delegate

Source: Internet
Author: User

 First create a very simple delegate ~ 
public delegate void DoIt (String str);
Then create a delegate reference ~

Public DoIt _doit;
The
then executes the delegate where your program needs to trigger the callback function ~

if (_doit! = NULL)//execute delegate needs to determine whether it is empty, if the corresponding signature method is not stored in the delegate, the direct execution will be error
{
_doit ("Test Test ");
}
The last step is to add a callback method to the delegate, the signature must be the same ~
_doit = (str) =//Here is an anonymous function, of course, you can also direct the method
{
//Here is the code executed when the delegate is triggered

}; don't understand please ask ~

========================================================================
C # This thing in general refers to a delegate. In fact, it can also be an interface.

Delegate void Workdone ();
void working (Workdone callBack) {
//working code.
Execute this delegate when the work is done.
CallBack ();
}
void Workdonehandler () {
//do something other.
}
Void Do () {
Workdone callBack = new Workdone (workdonehandler);
Working (CallBack);
}

is rather a method (Workdonehandler ()) passed as a parameter to another method (working (Workdone callBack)).
The advantage of this is that you can dynamically specify which method to execute.
For example, in the Do () method, the callback that we specify is Workdonehandler, and of course it can be a different method of matching. The working () method does not need to know which handler you are finally performing.

C # Delegate

Related Article

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.