Action and func delegate

Source: Internet
Author: User

Generic action <t> delegation and func <t> delegation are two generic delegation defined by the system.

Action <t> delegate indicates referencing a method with a return type of void. This delegate has different variations and can pass up to 16 different parameter types. At the same time, action classes without generic parameters can call methods without parameters. For example, action <in T> indicates a method to input parameters. Action <in T1, in T2> indicates two input parameters.

Func <t> can be used in a similar way. However, func <t> allows calling methods with return parameters. Func <t> also has different variants. up to 16 parameters and a return type can be passed. For example, the func <out tresult> delegate type can have a method with the return type without parameters. The func <in T1, int2, out tresult> indicates a method with two parameters and a return type.

Remember one thing. There can be multiple T types in action <t>, but these T types indicate differentInputType. Func <t> can represent methods with output, t can have multiple, and onlyLast outputThat is, the last is the return type. In func <in T1, int2, out tresult>CodeIt does not appear. In vs, you can view it through intelliisense:

16 parameter encapsulation of Action <t>:

 

Func <t> 16 participates in a returned Encapsulation

 

 

 

A simple code below demonstrates the similarities and differences between the two generic delegation and General Delegation.

Step 1: Define two functions first:

 
Public DoubleMultiplybytwo (DoubleX ){ReturnX * 2 ;}Public DoubleSquare (DoubleX ){ReturnX * X ;}

 

These two functions share the same feature: the input and return types are both double.

Step 2: Define the delegate array and use the method names of the two methods to initialize the array:

Delegate DoubleDoubleop (DoubleX); doubleop [] mydoubleop = {This. Multiplybytwo,This. Square}; func <Double,Double> [] Myfunc =//{This. Multiplybytwo,This. Square };

 

Last: View output

For(IntI = 0; I <mydoubleop. length; I ++) {console. writeline (mydoubleop [I] (1.414 ));}For(IntI = 0; I <myfunc. length; I ++) {console. writeline (myfunc [I] (2.236 ));}

 

In fact, the use of generic and custom delegation is no different. However, the generic commissioned func <t> system has been defined for us and can be used directly. We do not need to define delegate double doubleop (Double X );. The use of another generic delegated action <t> is the same, but there is no return type.

Paste the complete code:

A simple class myclass class:

 Public   Class Myclass { Delegate   Double Doubleop (Double X ); Public   Double Multiplybytwo ( Double X ){ Return X * 2 ;} Public   Double Square ( Double X ){ Return X * X ;} Public   Void Mydelegate () {doubleop [] mydoubleop = { This . Multiplybytwo, This . Square }; For ( Int I = 0; I <mydoubleop. length; I ++) {console. writeline (mydoubleop [I] (1.414);} func < Double , Double > [] Myfunc = { This . Multiplybytwo, This . Square }; For ( Int I = 0; I <myfunc. length; I ++) {console. writeline (myfunc [I] (2.236 ));}}}

 

Entry function:

 
ClassProgram {Static VoidMain () {eventssample. myclass =NewEventssample. myclass (); myclass. mydelegate (); console. Read ();}}

 

Technorati Tag: Action <t>, func <t>, delegate

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.