There are two ways to access data or call Methods: one is to access or call data by name, and the other is to access the call through the memory address. To ensure system security and stability, the NET Framework CLR library does not allow programs to directly operate data or methods in the memory through pointers, instead, you can use a hosting mechanism to access data in the memory or call methods in the memory. A delegate is a special data type provided by C # That calls a method using a hosting mechanism. It acts on a house rental agency.
What we want to do below mainly involves the following three points:
// 1. Define the delegate
// 2. Delegation instantiation
// 3. The delegate call actually directs the delegate to a implemented method.
// Note: The returned type of the called method must be the same as the returned type of the defined delegate.
A delegate is a class that defines the type of a method so that the method can be passed as a parameter.
I. Definition of delegation
Use the keyword delegate in C # To declare the delegate. The general form of declarative delegation is:
// Definition of delegation
// [Access modifier] delegate name of the delegate data type (parameter list ....)
Before using delegation, you must first define
Example: Delegate void d (int x)
Ii. instantiation (binding with methods)
To be used together with the naming method, the delegate must be instantiated using a method with an acceptable signature.
The instantiation method can be any of the following methods
The static method referenced in the delegate creation expression, or the target object referenced in the delegate creation expression (this object cannot be null) and the instance method is another delegate
For example:
Delegate void d(int x) delegate void D(int x);class C{ public static void M1(int i) {...} public void M2(int i) {...}}class Test{ static void Main() { D cd1 = new D(C.M1); // static method Test t = new C(); D cd2 = new D(t.M2); // instance method D cd3 = new D(cd2); // another delegate }}
Iii. Delegate call
After a delegate object is created, it is usually passed to other code that calls the delegate. The delegate object is called by the name of the delegate object (followed by the parameter to be passed to the delegate, which is enclosed in brackets. The following is an example of a delegated call:
Public delegate int MathOp (int I, int j); // defines the delegate class DelegateTest {public static int add (int I, int j) {// method return I + j ;} public static int Mutiply (int num1, int num2) {// return num1 * num2;} static void Main (string [] args) {MathOp mo = new MathOp (add ); // delegation instantiation, pointing to the add method MathOp maOp = new MathOp (Mutiply); // delegation instantiation, pointing to the Mutiply method Console. writeLine (mo (10, 20); // delegate the call Console. writeLine (maOp (4, 5); // delegate the call Console. readLine ();}}}
Cognition:
In the past, we only knew that a delegate is a method call. Through this project instance, we learned that the delegate also needs to be defined, instantiated, and called. I still did not take advantage of previous learning skills. The use of delegation is usually used in concert with anonymous functions or lambda expressions. The next section describes anonymous functions.