c#3.0 notes (i) delegate of preparatory knowledge

Source: Internet
Author: User
Tags new features

Before you study c#3.0, you should review the delegates and events first, as this will help you to understand some of the new features in c#3.0, such as Lambada expressions.

Background

In C, we can use function pointers to create callback functions, but there are some security problems with the callback function in C. Because it is recorded only in the memory address, there is no such other security information as the parameter type of the method, the number of parameters, the return value, and so on. In the. NET Framework, callbacks are still available, and the. NET Framework provides a more advanced, more secure object-oriented delegate to implement.

Defining delegates

There are three important information that is included in the delegate:

1. The name of the method invoked;

2. Parameters of the method;

3. The return value of the method.

To define a simple delegate:

public delegate int Caculate(int x,int y)

Such a delegate is a signed parameter with a value of two int that returns an int type, an object of type Caculate can dynamically call its pointed method at run time. Note that the. NET delegate can either point to a dynamic method or point to a static method.

When the C # compiler handles delegate, it automatically generates a class that inherits from System.MulticastDelegate. It is this kind of root system.delegate that provides the necessary basic information for delegates to maintain a list of methods that need to be invoked. We can see through the IL Viewer:

Three methods are defined in the generated Caculate class: BeginInvoke, EndInvoke, Invoke. Where invoke is the core method, which is used to invoke each method in the delegate list in a synchronized fashion. We can look at how the compiler defines these methods. The parameters in the Invoke method are exactly the same as the return value as the Caculate delegate, while the BeginInvoke has two more arguments one is of type AsyncCallback type, and the EndInvoke method returns the int type.

Get more information through Multicastdeletate and delegate base classes

I can also see from the IL code above that the compiler generates the delegate when the class is inherited from the MulticastDelegate, and the MulticastDelegate inherits from the delegate class, so you can use these two classes to get the delegate more auxiliary information. There are only a few common properties and methods listed here, and you can get more of the content of these two classes on MSDN (MulticastDelegate members).

1.Methos property: Returns the System.Reflection.MethodInfo type that describes the method information represented by the delegate.

2.Target property: Returns the object that the delegate method is in, or null if it is a static method.

3.GetInvocationList method: Returns an array of delegate types, where each element of the array represents a method that can be invoked.

4.Combine method: A static method is used to add a method to a delegate.

5.Remove method: A static method removes a method from the delegate.

Related Article

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.