Letter of authorization
Delegate: similar to enumeration, it can be defined in the class or namespace.
Declaration: public delegate int Entrust (int x, int y); note: the parameter passed in when the delegated instance is created is the method name.
Class MyMath {public int add (int x, int y) {return x + y;} public int sub (int x, int y) {return x-y ;}}Preparation
Use: Entrust e = new Entrust (MyMath. add); Note: static methods can be used like this; otherwise, create an object of MyMath first.
Multicast delegation: e + = MyMath. sub; Note: it is best not to use multicast Delegation for methods with return values.
Anonymous delegate: e + = delegate (int x, int y) {return x * y ;};
Lambda: e + = (x, y) => x/y; Note: build tomorrow
Use: Console. WriteLine (e (8, 2 ));
Pre-defined commonGeneric Delegation:
Func delegate: there are multiple overload types: delegate TResult Func <T1, T2, T3, T4, TResult> -- Func <string, int> f = delegate (string x) {return 1 ;}; Note: The last parameter of the generic type is the return value type.
Action delegate: no return value relative to Func delegate
Predicate delegation: determines whether a parameter meets a certain standard. Only one parameter can be accepted. The parameter type is specified in <> and the return value is bool. Predicate <string> p = delegate (string x) {return true ;};
Comparison delegate: Compares two elements. The two parameters must be of the same type. The parameter type is specified in <> and the return value is int, which is generally used for sorting. Comparison <string> c = delegate (string x, string y) {return 1 ;};