1. Delegate
1. We all know that the usage of pointers in C/C ++ is flexible and has the following advantages:
A. provides a flexible means for the function to modify and call the variable;
B. Support C Dynamic Allocation subroutine
C. It can improve the efficiency of some subprograms.
> During data transmission, if the data block is large (such as a data buffer or a large structure), you can use the pointer transmission address instead of the actual data to increase the transmission speed, saves a lot of memory.
D. Support for dynamic data structures (such as binary trees and linked lists)
Although C # does not support pointers, the delegation solves this problem well. The upper is the difference between delegation and pointer:
A. function pointers are insecure types, while delegation is fully object-oriented and type-safe.
B. function pointers mostly point to member functions, while delegation encapsulates object instances and methods.
An interesting phenomenon of a delegated instance is that it does not know or care about the class to which the encapsulated method belongs. It only cares about these methods and must be compatible with the delegate type. Therefore, delegation is very suitable for "anonymous" calls.
1.1 declaration Commission
Use the keyword delegate to declare the delegate. The form is as follows:
[Feature] [modifier] delegate [Return Value Type] [Delegate name] ([Form parameter list ])
[Features], [modifier]: Optional
[Return Value Type], keyword delegate, [Delegate name]: Required
[Parameter list]: Optional
The delegate name of C # is a medium identifier. Naming rules: generally use uppercase letters.
● They have the same signature, number of parameters, type, order, and modifier.
● The return value type is the same.
A delegate defines a reference type, encapsulates a reference to a method, and defines a signature and a return value type.
For example, we define a list of parameters that return values of the int32 type as object and int32.
Delegate int32 mydelegate (Object Val, int32 I );
The following method is compatible with mydelegate
Int32 mymethod (Object Val, int32 I );
For delegates: even if they have the same parameter list and return value type, they are considered to be two different delegates. Delegation principle: the name is equivalent, not the structure is equivalent.
1.2 delegated instance
[Delegate name] [delegate instance name] = new [Delegate name] ([parameter list ]);
The [parameter] must be a method name or a delegate name (to put it bluntly or a method). It can be a static method but must be compatible with the delegate type. If the parameter is the delegate name, it identifies the delegated instance for which the copy is created in Anhui.
1.3 Use Delegation
Delegate. CS
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace delegate {// declare the delegate Public Delegate int printmessagehandler (string message); // define the test class public class test {public static int print (string mesg) {console. writeline (mesg); Return mesg. length;} // use the delegated instance as the parameter (function as the parameter) public static void printtitle (printmessagehandler PRN) {PRN ("=========================== "); PRN ("delegated instance as parameter "); PRN ("=========================== ");}} class program {static void main (string [] ARGs) {test = new test (); // instance 1: the object that delegates passing parameters (I think it is actually similar to the C ++ function pointer) printmessagehandler PRN = new printmessagehandler (test. print); string MSG = "delegate instance Parameters"; // MSG = console. readline (). tostring (); int Len = PRN (MSG); // int len1 = test. print (MSG); console. writeline ("parameter length:" + Len); // console. writeline ("parameter length:" + len1); test. printtitle (PRN); console. readline ();}}}
Running result: