From the preface, we should know a little bit about the importance of the Commission and the event, although we can still blur it, but from my plain English series, I will make these concepts easy to understand. First of all, we should first talk about the Commission, literally understand, as long as the Chinese should know this meaning, unless the commission of the 2 characters do not know, for example, Xiao Ming entrusted to buy a ticket.
But is it so simple in our program world? Of course, what is Oo,oo is the real world of abstraction, all the real world things, in OO can get a very good interpretation, sorry, pull away ...
Then we straightforward, if the students do not wearies, you can look at the online Chinese, the English greetingpeople example.
The first is the C # syntax
public delegate void Bugticketeventhandler ();
Delegate is the key word, "Note: EventHandler is a statement commissioned by the Microsoft C # naming standard, my habit is the standard will be the first time to say that the naming habit to be the first time to develop"
It's over, this is the Commission, it's so simple, see what the difference is with our general approach?
public void Buyticket () { method body}
The difference is that in real life, a delegate is just a command, to do something else, and in the program world, the delegate just stores the address of each method, and he does nothing.
Then we put that, Xiao Ming commissioned a small Zhang to buy tickets to the real-life scene, how in the program world embodiment?
Code highlighting produced by Actipro Codehighlighter (freeware) http://www. codehighlighter.com/-->//public class Mrzhang { //actually buy tickets for the sad character is Xiao Zhang public static void Buyticket () { Console.WriteLine ("NND, every time let me go to buy tickets, chicken!") "); } } Xiao Ming class mrming { //Declare a delegate, which is actually a "command" public delegate void Bugticketeventhandler (); public static void Main (string[] args) { //Here is a concrete explanation of what this command is for, this example is Mrzhang.buyticket "Xiao Zhang Buy ticket" Bugticketeventhandler mydelegate = new Bugticketeventhandler (mrzhang.buyticket); At this time the Commission was attached a concrete method mydelegate (); Console.readkey (); } }
Bugticketeventhandler mydelegate = new Bugticketeventhandler (mrzhang.buyticket);
This is the declaration method of the delegate, Bugticketeventhandler (the delegate method), and the delegate's method must be added because the delegate's constructor is not empty.
Note: The parameter and return type of the delegate must be the same as the method you want to delegate, for example:
public delegate void Bugticketeventhandler ();
public static void Buyticket ()
{
Console.WriteLine ("NND, every time let me go to buy tickets, chicken!" ");
}
Students, see here can digest digestion first, rest, we simply talk about the concept of the chain of trust:
In fact, the delegation chain is quite simple, in real life, Xiaoming called Xiao Zhang to buy the ticket, may then let him take a movie ticket,
And the expression in our program World is:
Code highlighting produced by Actipro Codehighlighter (freeware) http://www. codehighlighter.com/-->//public class Mrzhang { //actually buy tickets for the sad character is Xiao Zhang public static void Buyticket () { Console.WriteLine ("NND, every time let me go to buy tickets, chicken!") "); } public static void Buymovieticket () { Console.WriteLine ("I'll go, pick up my own girls, and let me bring a movie ticket!") "); } } Xiao Ming class mrming { //Declare a delegate, which is actually a "command" public delegate void Bugticketeventhandler (); public static void Main (string[] args) { //Here is a concrete explanation of what this command is for, this example is Mrzhang.buyticket "Xiao Zhang Buy ticket" Bugticketeventhandler mydelegate = new Bugticketeventhandler (mrzhang.buyticket); MyDelegate + = Mrzhang.buymovieticket; At this time the Commission was attached a concrete method mydelegate (); Console.readkey (); } }
In fact, we just added mydelegate + = Mrzhang.buymovieticket in the program;