Commissioned
A delegate is a data type, like a class (which can declare a delegate type variable), which can be an int string class type.
1
Commission Summary
1 defining a delegate requires delegate keyword
public delegate void Writetimetoany ();
2 The method that the delegate uses to store is consistent with the defined delegate type
3 A delegate is a data type that needs to be passed a variable, either using the keyword new or not using the
Meaning of using delegates
The main is to inject code, write a delegate type method to facilitate later use can be used to implement the corresponding function
2
System time Representation Method File.writealltext (@ "Time.txt", System.DateTime.Now.ToString ("Yy-mm-dd hh:mm:ss"));
3
Use of class libraries
1 Defining a class library
2 Adding a reference to a class library in a new project
3 Add a class library namespace to use the class library
Delegate instance:
1 Defining a class library and a delegate
1 namespaceClassLibrary12 {3 Public classClass14 {5 //declaring a delegate6 PublicWritetimetoany writetime;7 Public voiddosometing ()8 {9Console.WriteLine ("---------------------------------------------------------------------");TenConsole.WriteLine ("---------------------------------------------------------------------"); One writetime (); AConsole.WriteLine ("---------------------------------------------------------------------"); -Console.WriteLine ("---------------------------------------------------------------------"); - } the } - } - - + namespaceClassLibrary1 - { + //Public class Writetimetoany A //define a delegate that has no parameters with no return value at Public Delegate voidWritetimetoany (); - -}
Using a delegate
1 class Program2 {3 Static voidMain (string[] args)4 {5Class1 CLS1 =NewClass1 ();6Cls1.writetime =M1;7 CLS1. Dosometing ();8 Console.readkey ();9 }Ten One Static voidM1 () A { -Console.WriteLine ("System Current time: {0}", System.DateTime.Now.ToString ("YY-MM-DD Hh:mm:ss")); - the } - } -}
Use Delegate two
1 class Program2 {3 Static voidMain (string[] args)4 {5Class1 CLS =NewClass1 ();6Cls.writetime =M1;7 CLS. Dosometing ();8 9 Console.readkey ();Ten One } A Static voidM1 () - { -File.writealltext (@"Time.txt", System.DateTime.Now.ToString ("YY-MM-DD Hh:mm:ss")); theConsole.WriteLine ("OK"); - } -}
callback function:
function Invoke_and_add (A, b) {
Return a () +b ();
}
function One () {
return 1;
}
function () {
return 2;
}
Invoke_and_add (one, both);
Invoke_and_add (function () {return 1;},function () {return 2;})
With the above two instances, the callback function is defined as passing a function A to another function B, and this function B executes function A. Let's just say that function A is called a callback function. If there is no name, it is called an anonymous callback function
function of the callback function
There are three main
1. When a function is passed as a parameter, there is no need to define a name for the function, which has the advantage of reducing the global variable.
2. Save a lot of code.
3. Improve the performance of the program.
Pointer functions
Defined
Edit
First it is a function, except that the return value of this function is an address value. The function return value must be accepted with a pointer variable of the same type, that is, the pointer function must have a function return value, and in the keynote function, the function return value must be assigned to a pointer variable of the same type.
2 Defining the Format
Edit
Type name * Function name (function argument list);
Where the suffix operator parenthesis "()" means that this is a function whose prefix operation Asterisk "*" means that this function is a pointer-type function whose function value is a pointer, that is, the type of the value it brings back is a pointer, and when this function is called, a "point to return value of ..." is given. Pointer (address), "type name", which indicates the type "of the pointer returned by the function."
The parentheses in the (function argument list) are the function call operators, and in the calling statement, even if the function has no arguments, the parentheses of the parameter table cannot be omitted. The examples are as follows:
int *pfun (int, int);
Because the priority of "*" is lower than the priority of "()", so Pfun first and later "()", it means that pfun is a function. That
int * (pfun (int, int));
Then, with the previous "*", it shows that the return value of this function is a pointer. Since there is also an int in front, that is, Pfun is a function that returns a pointer to an integral type.
The return type can be any base type and composite type. Functions that return pointers are very versatile. In fact, every function, even if it does not carry a pointer that returns a certain type, itself has an entry address that is equivalent to a pointer. For example, the function returns an integer value, which is actually equivalent to returning the value of a pointer variable, but the variable is the function itself, and the entire function is equivalent to a "variable".
Delegate & pointer functions & callback functions