The following is my understanding of delegation and events. If you have any questions, please correct them. Thank you!
I. Delegation
1 Definition
Aside from the meaning of the book, entrusting is actually a method proxy.
Example: for example, you need three people to do one thing separately, but now you have the agents of these three people (the supervisors or superiors of these three people ), the only thing that needs to be done is to tell the code owner what they should do, and then the agent will automatically tell them what they should do.
A delegate is a type that can be defined inside the class or outside the class. Its keyword is delegate.
2. Use
For the use of delegation, you only need to remember the following steps:
1. Declare a commission;
② Define a delegate variable;
③ Specify the "proxy" Method for the delegate variable;
④ Execute this delegate
Example: for example, you now have three printers (one person in your life probably won't use three at the same time. Here is just an example.) a, B, c, each printer has only one method, that is, printing, as follows:
Class printa {public void printapaint () {console. writeline ("Printer A printing");} class printb {public void printbpaint () {console. writeline ("Printer B printing");} class printc {public void printcpaint () {console. writeline ("Print Printer No. c ");}}
Now you don't want to manipulate a printer on a single platform. If you want to use a delegate to manipulate the printer, you need to create a delegate. We are in a controller (Controller class) first, you must declare a delegate as follows:
// Step 1: declare delegation (Note: delegation is a concept parallel to "class". It can be declared inside the class or outside the class, similarly, before using a class, you must first create a class) Public Delegate void printpaintdelegate ();
Secondly, you have to define a delegate variable (Note: you can think of classes here. When you use a class, you must first create a new one. Here we are "new" and a delegate)
// Step 2: Define the delegate variable public printpaintdelegate ppaintdelegate;
Next, you have to define the deletion method for your delegate (Note: The delegate is actually a proxy. to delegate, you need to provide it with the proxy method)
// Step 3: Pass the method that you want to call by Delegate to the delegate variable. You can pass it to multiple delegate Methods // Add public void add (printpaintdelegate PPD) {ppaintdelegate + = PPD;} // remove a public void remove (printpaintdelegate PPD) {ppaintdelegate-= PPD ;}
Step 3: add and delete methods for the delegate. This method must be the same as the number of parameters, parameter types, parameter types, and return values of the Delegate. Otherwise, compilation fails, here, the add and remove methods are not required. You can also directly use ppaintdelegate + = PPD; or ppaintdelegate-= PPD; to add and delete methods.
The delegation and related methods have all been defined at this time. This is the time for the main method to come out, and directly go to the Code:
// Initialize the relevant variables printa Pa = new printa (); printb Pb = new printb (); printc Pc = new printc (); // define the Controller Col = new controller (); // Add the method Col for the controller. add (Pa. printapaint); Col. add (Pb. printbpaint); Col. add (PC. printcpaint); // execute the delegate Col. ppaintdelegate (); console. readline ();
After running the program, the output result is as follows:
Printer A is printing
Printer B is printing
Printer C is printing
Note: The operations for executing the delegate are the same as those for executing the method. If there is a parameter, the parameter is passed. If there is no parameter, no parameter is required.
At last, the delegation came into being to serve events. what I know about its main application is events, therefore, when you ask "What is the main purpose of Delegation?", you will silently answer: "It appears to serve events ",:-), the above is the understanding of delegation.
Ii. Events
An event is a Special Delegate. The use of the event is not much different from that of the Delegate. The procedure is as follows:
1. Declare a commission;
② Define an event variable;
③ Specify the "proxy" Method for the delegate variable;
④ Execute this event
In this example, we also use the printer event that we just defined. We will not talk about the printer printing method here. We will only talk about one controller class and directly go to the Code:
// Delegate related operations // Step 1: declare the delegate Public Delegate void printpaintdelegateevent (); // Step 2: Define the event variable public event printpaintdelegateevent ppaintdelegateevent; // Step 3: you can pass the method that you want to call by Delegate to the delegate variable. You can pass the delegate multiple methods // Add the public void add (printpaintdelegateevent PPD) {ppaintdelegateevent + = PPD ;} // remove a method public void remove (printpaintdelegateevent PPD) {ppaintdelegateevent-= PPD;} // The execution method public void run () {This. ppaintdelegateevent ();}
Note: In the second step, the keyword even is added when the delegate variable is defined. This is one of the differences between the delegate and the event. Let's look at our main method:
// Initialize the relevant variables printa Pa = new printa (); printb Pb = new printb (); printc Pc = new printc (); // define controller Col = new controller (); // Add method Col for controller. add (Pa. printapaint); Col. add (Pb. printbpaint); Col. add (PC. printcpaint); // Delete the method Col in a delegate. remove (Pb. printbpaint); // events cannot be called externally like delegates, that is, they cannot be called as follows, even if the method is public // Col. ppaintdelegate (); // The Code is as follows: Col. run (); console. readline ();
In this main method, the difference from the previous delegate case is that when an event is executed, it cannot pass Col. ppaintdelegate (); execution, but can only call the event (delegate) within the class defining the event. This is the second difference between the event and the delegate.
It can be understood that an event is a special delegate that can only be executed within the class that defines the event.
However, there is another problem here. Why is the event basically the same as the delegate, and we need to distinguish between the event and the delegate? In fact, I was not quite clear about this problem at the beginning. Later I checked the information to find out why:
A delegate variable, such as the above ppaintdelegate, can be called inside the Controller class or inside the class, but I don't know if you have thought about a problem. If someone is not small inside the control class, when initializing this class, directly added a private method defined in the Controller for the delegate variable and executed it in the main method. How can the private method inside the class be private? The emergence of the incident is trying to make up for this shortcoming. I think you should understand what is going on.
Code: http://download.csdn.net/detail/hymhblf/4098735