Delegate, Predicate, Action, and Func

Source: Internet
Author: User

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(                                   }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.