Delegate in C #

Source: Internet
Author: User

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?

 

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.