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