In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.