What is a delegate (delegate)

Source: Internet
Author: User

Delegate (delegate), it's hard to understand when you first learn, we can think of it as a modifier that modifies the type of a class, just like public, static, and so on. Defining a delegate class is similar to our usual definition class. Let's take a look at the definition of the EventHandler class that we often use, which is. One of the classes in net:

Code
public delegate void EventHandler(object sender, EventArgs e);

The above code can be seen as defining a delegate class whose name is EventHandler. Here's how I define an ordinary class to see what the difference is between them:

Code
public class UserInfo{}

As you can see, the definition of delegate class and ordinary class is still quite different, just remember its format. Let's take a look at the instance of creating the class:

Code
UserInfo userInfo = new UserInfo();
EventHandler handler = new EventHandler(MethodName);

In code, it's no different than creating an instance of an ordinary class, except that it must have parameters and only one. This parameter is very special, it is not the type of parameter, but the name of the method. There are many types of methods (this is not public,static and so on), just as there are many types of arguments, it can't be accepted in any way. That's for sure, what kind of approach does it accept? This needs to be decorated.

Above, we see that the classes that define delegate are very different. There is a void behind it, and there are two more parameters in front of it. You may have thought of the type of method used to modify it. It defines that the return value of the method must be void, and its argument is (object sender, EventArgs e), as long as the method that meets both requirements can be passed to it.

The above code can also be written simply: EventHandler handler= methodname; So, does it seem like a method pointer to an instance of this assignment? After the assignment we can call it like a method, such as: Handler (sender, E); , calling handler is equivalent to calling the method methodname. The process of calling MethodName through handler is called an agent, or it can be called a delegate, that is, the MethodName delegate to handler.

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.