This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18347|Reply: 0

[Communication] Delegate in C#

[Copy link]
Posted on 2/7/2019 9:08:45 PM | | |
1. What is entrustment?

A delegate is a reference type, which is a managed version of a function pointer. In C#, delegates are a type that stores references as functions. Delegates can reference both instances and static methods, while function pointers can only reference static methods. The delegate declaration is very similar to a function, unlike a function in that the delegate does not have a function body and requires the Delegate keyword. The delegated declaration specifies a function signature that contains a list of parameters and a return type. Once the delegate is defined, a variable of the delegate type can be declared, which can then be initialized as referenced by a function with the same signature as the delegate, which can then be called using the delegate variable. (ps: In fact, he entrusted him with a class, and his purpose was to pass the method as a parameter to other methods!) )

Delegates, while very similar to function pointers, are not pointers. Many people put . The delegate in .NET is understood as a safe function pointer, which is far-fetched, and the function pointer of the delegate implementation is very similar in that it provides a program callback mechanism.

2. Four steps of entrustment: 1. Declare the type of entrustment

  2. There is a method that contains the code to be executed

  3. Create a delegated instance

  4. Call the delegate instance

3. Examples:






Previous:[DllImport("user32.dll")] and extern usage in C#
Next:Pure C# hook implementation
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com