You can only trust this thing. If you cannot understand it, you may feel uncomfortable. However, I believe that one day you will suddenly realize it.
I suddenly realized it, so I will talk about it.
In fact, the purpose of delegation is to adapt to unpredictable things.
Just like you put a button on the form, God knows what you want to do? But God knows what you want to do! Now that you know what you want to do, what you want to do must be related to this button. At this time, the Delegate will appear, and the operating system will entrust you with what you need to do by clicking this button. The operating system writes a method name (delegate name), uses the button and click event as a parameter, and then completes the method. For example, you want to bring up a dialog box, as shown below:Code:
Private void btnstartlistening_click (Object sender, eventargs E)
{
Msgbox. Show ("Hello, world! ");
}
Object sender and eventargs E. The first two parameters are the event source (button) and the second parameter is the event (click ), then you can use these two parameters to implement what you want to do. Of course, you can also use these two parameters (not nonsense ).
The delegated system is ready for you, and even the methods you need to write are arranged by the system. All you need to do is to add "msgbox. Show (" Hello, world! ");". When running, the system will pass the "btnstartlistening_click" method as a parameter to a delegate instance.
At this time, you should understand that the events in C # are implemented by delegation.
Do you understand?