. NET Note: Analysis of Internal principles of Delegate

Source: Internet
Author: User

First, we need to figure out what is delegation? I believe everyone is familiar with delegation. Delegation is actually a type of method signature. For details about delegation, refer to the description in MSDN. The link is http://msdn.microsoft.com/zh-cn/library/vstudio/ms173171.aspx. I will not introduce it here;

In this article, I mainly want to tell you the internal structure of the Delegate. Let's first review the delegate through a simple demo:
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;

Namespace _ 01 Delegate
{
// Indicates the method signature, that is, a strong pointer.
Public delegate int AddDele (int a, int B );

Class Program
{
Static void Main (string [] args)
{
// Define a delegate variable. Note that + = must be followed by the new Keyword when pointing to the first method.
AddDele delStatic = new AddDele (Add );
// Use delegate: Delegate the static method
Console. WriteLine (delStatic (3, 4 ));

// Delegate the instance method
Program p = new Program ();
AddDele delInstance = new AddDele (p. AddInstance );
// Output: 9
Console. WriteLine (delInstance (4, 5 ));

Console. ReadKey ();
}

// Static method
Static int Add (int a, int B)
{
Return a + B;
}

// Instance method
Public int AddInstance (int a, int B)
{
Return a + B;
}
}
}

In the above Code, the static method and the instance method are called using the delegate respectively. If this code looks very difficult, we recommend that you consolidate the relevant content of the Delegate with the link provided above.

 

Focus: internal structure of delegation

The delegate can be divided into three parts: _ target, _ methodPtr and delegate chain. (take the demo above as an example)

_ Target: As the name implies, it is the delegate target function. If it is a static method, _ target is null. If it is an instance method, _ target points to the current instance, in the above example, the value of delInstance _ target is p (the instance of Program );

_ MethodPtr: Method pointer pointing to the address of the method in memory;

Delegated chain: the delegated chain formed by the plus sign (+ =) operation actually points to methods;

I drew a simple figure to describe the internal structure of the delegate: (as follows)

These are my own understandings. If there are any mistakes, you are welcome to point out and discuss and learn from each other. I hope this article will help you understand the delegation, at the same time, we also record our learning accumulation to improve ourselves.

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.