Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of the Internal principle of Delegate in .NET

Shulou Source: shulou.com Published: 2022-06-03 05:55:58 11月12日 Update

Editor to share with you an example analysis of the internal principles of Delegate in .NET, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

First review the delegate through a simple demo:

The code is as follows:

Using System

Using System.Collections.Generic

Using System.Linq

Using System.Text

Using System.Threading.Tasks

Namespace _ 01Delegate

{

/ / the signature of the constraint to the method, that is, a strongly typed pointer

Public delegate int AddDele (int a, int b)

Class Program

{

Static void Main (string [] args)

{

/ / define a delegate variable. Be sure to use the new keyword after the + = when pointing to the first method.

AddDele delStatic = new AddDele (Add)

/ / use delegate: delegate of static method

Console.WriteLine (delStatic (3,4))

/ / delegation of 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 delegate is used to call the static method and the instance method, respectively. If this code still looks laborious, it is recommended to consolidate the relevant content of the delegate according to the link I provided earlier.

Starting point: the internal structure of the delegate

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

_ target: as the name implies, it is the target function of the delegate. 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 _ target of delInstance is p (instance of Program).

_ methodPtr: method pointer, pointing to the address of the method in memory

Delegate chain: the delegate chain formed by + = operation actually points to individual methods.

I simply drew a picture to describe the internal structure of the delegate: (following)

The above is all the content of the article "sample Analysis of the Internal principles of Delegate in .NET". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Delegate method instance point static code content that is article principle example analysis pointer effort as the name implies three not much that is keywords Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info MySQL vpn Microsoft