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> does not return a 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 <tresult>
2. Func <t, tresult>
3. Func <T1, T2, tresult>
4. Func <T1, T2, T3, tresult>
5. Func <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 encapsulation methods.
Private Bool Checkisint32 ( Int Pars) // Encapsulated Method
{
Return Pars = 5 ;
}
Bool OK = myfunc ( 5 ); // Call delegate
Msdn: 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: 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; // 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.