Letter of authorization
I. Initial Knowledge Delegation
1. in C #, the delegate is a special reference type. The delegate is equivalent to a pointer of the method, and the delegate and class are of the same level.
2. Syntax:
Access modifier delegate return type name (parameter list );
Eg:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace ConsoleApplication1 8 {9 public delegate double multiOp (double x); 10 class Program11 {12 // method 1 evaluate square 13 static double Multiply (double x) 14 {15 return x * x; 16} 17 // method 2 returns the open side 18 static double Sqrt (double x) 19 {20 return Math. sqrt (x); 21} 22 23 // method 3 delegate as the 24 static double Operation (double x, multiOp ops) 25 {26 return ops (x ); 27} 28 29 static void Main (string [] args) 30 {31 // instantiate a delegate object 32 // instantiate the delegate and call 33 multiOp compute = new multiOp (Multiply ); 34 Console. writeLine ("{0}'s Square is {1}", 4, compute (4); 35 36 // instantiate the delegate and call 37 compute = new multiOp (Sqrt ); 38 Console. writeLine ("{0} open for {1}", 16, compute (16); 39 40 // do not instantiate the delegate 41 multiOp compute2 = Multiply; 42 Console. writeLine ("{0}'s Square is {1}", 4, compute2 (4); 43 44 compute2 = Sqrt; 45 Console. writeLine ("{0} is opened by {1}", 16, compute2 (16); 46 47 // delegate as the parameter of the method 48 Console. writeLine ("delegate as method parameter:" + Operation (2, Multiply); 49 50 Console. readLine (); 51} 52} 53}
Ii. Anonymous Method
1. The Syntax Mechanism of the anonymous method is mainly to simplify the use of the delegate object.
2. Syntax:
Delegate (parameter list) {expression or statement block}
Eg:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace ConsoleApplication2 8 {9 public delegate double multiOp (double x); 10 class Program211 {12 // method delegate as the 13 static double Operation (double x, multiOp ops) 14 {15 return ops (x); 16} 17 18 static void Main (string [] args) 19 {20 multiOp mp = delegate (double x) 21 {22 return x * x; 23}; 24 25 // 1. use method parameters as parameters 26 Console. writeLine ("delegate as method parameter:" + Operation (2, mp); 27 // 2. use the method parameter as the parameter 28 Console. writeLine ("delegate as method parameter:" + Operation (2, delegate (double x) {return x * x ;})); 29 30 Console. readLine (); 31 32} 33} 34}
Iii. Lamda expressions
1. Lamda expressions are provided in C #3.0 to further simplify the use of delegation
2. Syntax:
(Parameter list) => expression or statement Block
(X) =>{ return x * x}
(X) =>{ return r = x * x; return r ;}
Parameter List: It can be multiple parameters, one parameter, or no parameter. It must be consistent with the delegate parameter.
=>: Operator, indicating the parameter passed to the expression
Expression or statement block: Implementation part of the method (method body)
Eg:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace ConsoleApplication3 8 {9 public delegate double multiOp (double x); 10 class Program311 {12 // method delegate as the 13 static double Operation (double x, multiOp ops) 14 {15 return ops (x); 16} 17 18 static void Main (string [] args) 19 {20 // 1. use method parameters as parameter 21 Console. writeLine ("delegate as method parameter:" + Operation (2, (x) => x * x); 22 23 Console. readLine (); 24} 25} 26}
Iv. Generic Delegation
1. Some default delegate forms are provided in. NET.
Public delegate TResult Func <in T, out TResult> (T arg); // The last one is the output type, and the other is the input type.
Action <in T, in T1> (T arg); // all input types
Predicate <in T> (T arg );
Eg:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication4 8 { 9 class Program410 {11 static void Main(string[] args)12 {13 Func<double, double> ope = x => x * x;14 Func<int, bool> del = a => a > 10;15 16 17 Console.WriteLine(ope(10));18 Console.WriteLine(del(12));19 }20 }21 }