Introduction and usage of func and action

Source: Internet
Author: User

From: http://www.cnblogs.com/mashuping/archive/2009/03/26/1422108.html

Func is a type of Delegate, which is added in 3.5. in 2.0, we use the delegate to use delegate, and func is located in system. in the core namespace, the use of delegation can improve the efficiency. For example, using it in reflection can compensate for the performance loss caused by reflection.

Action <t> and func <t, tresult> have the same functions, but action <t> has no return type,

Func <t, t, result>: There are parameters and return types.

Action, no response or parameter is returned,

Func <t, tresult> has the following forms:

1. Func <t, tresult>

2. Func <t, T1, tresult>

3. Func <t, T1, T2, tresult>

4. Func <t, T1, T2, T3, tresult>

5. Func <t, T1, T2, T3, T4, tresult>

The meaning of each parameter is described separately. tresult indicates the type represented by the return value of the Delegate, T, T1, T2, T3, and T4 indicate the parameter type of the method called by the delegate,

The following is an example:

Func <int, bool> myfunc = NULL; // all variables

Myfunc = x => checkisint32 (x); // lambda expressions are used to delegate the encapsulation method.

Private bool checkisint32 (int pars) // encapsulated Method
{
Return pars = 5;
}

Bool OK = myfunc (5); // call the delegate

Msdn:The http://msdn.microsoft.com/zh-cn/library/bb534303 (vs.95). aspx

But what if we need to encapsulate the method without returning the value? Use action!

AvailableAction <T1, T2, T3, T4>The delegate transmits methods as parameters without explicitly declaring custom delegates. The encapsulated method must correspond to the method signature defined by the delegate. That is to say, the encapsulated method must have four parameters that are passed to it through values and cannot return values. (In C #, this method must returnVoid. In Visual BasicSub...End subStructure to define it .) Generally, this method is used to execute an operation.

The usage is similar to func!

Msdn:The http://msdn.microsoft.com/zh-cn/library/bb548654 (vs.95). aspx

Action: no response or parameter. The usage is as follows:

Action action = NULL; // define action

Action = checkisvoid; // The encapsulation method. Only the method name is required.

Action (); // call

Conclusion: The use of func <t, tresult> and action <t>, rather than delegate is to simplifyCode, Use less code to achieve the same effect, and we do not need to declare a delegate. The last parameter of func <t, tresult> is always the return type, while action <t, tresult> there is no return type, and action has no return type and parameter input.

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.