Preface
Callback Function Mechanism.
Delegate
Boolean delgate1( d1 = (d1( delegateMethod1( }
Type instance, the parameter is the method name, this also becomes subscription or registration, register a callback method for this delegate type, the following is called, it is so simple and complicated to create and call a delegate in C #, but these tasks are done by the compiler for us.
Predicate
Predicate< T>(T obj);
d1 = Predicate<> (d1( delegateMethod2( }
Action
[TypeForwardedFrom( Action< T1, T2>(T1 arg1, T2 arg2);
d1 = Action<> d2 = Action<, > d1( d2(, delegateMethod3( delegateMethod4( item, }
Func
[TypeForwardedFrom( TResult Func< T1, T2, TResult>(T1 arg1, T2 arg2);
Supports custom return value types. You can also see that the modifier before T1 and T2 is in, and the modifier before TResult is out, which are described below. Create call code:
hiddenMethodString = d1 = Func<, > d2 = Func<, , >(( item, hiddenMethodString; d3 = Func<, >((a) => a; d1( d2(, d3( delegateMethod5( }
It is actually a syntax specification. With the development of C #, it is constantly evolving.
Main (regular delegate Boolean delgate1 (d1 = (d1 (Console. writeLine (delegateMethod1 (Predicate delegate-custom parameter (only one parameter) d1 = Predicate <> (d1 (delegateMethod2 (Action delegate-custom parameter (multiple parameters, multiple types, but no return value) d1 = Action <> d2 = Action <,> d1 (d2 (, delegateMethod3 (delegateMethod4 (item, Func delegate-custom parameters (multiple parameters, multiple types, but there is a returned value) hiddenMethodString = d1 = Func <,> d2 = Func <,> (item, hiddenMethodString; d3 = Func <,> (a) =>; d1 (d2 (, d3 (delegateMethod5 (}View Code invert and collaborative change what is invert and covariance?
d1 = Console.WriteLine(d1( }
The parameter type () is the base class of the delegate parameter type (). The return type (String) is derived from the Delegate's return type (Object, compilation and running are acceptable, that is, they are allowed. The inverter and covariant can only be used for the reference type, not for the value type or void.
Inverter: The parameters obtained by the method can be the base class of the delegate parameter type.
: Return a type derived from the return type of the delegate.
How to Use inverter and covariant?
TResult MyDelegate< T, TResult> d1 = MyDelegate<FieldAccessException, > d1( }