Func is a delegate, which is added in 3.5, 2.0 inside we use the delegate is located under the System.core namespace, using a delegate can improve efficiency, such as in reflection can compensate for the loss of performance of the Delegate,func.
The function of action<t> and func<t,tresult> is the same, just action<t> no return type,
Func<t,t,result>: parameter, return type
Action, there is neither return nor parameter,
Func<t,tresult>
Forms of expression are divided into the following types:
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>
Say the meaning of each parameter separately, TResult said
The type represented by the value returned by the delegate, T,T1,T2,T3,T4 represents the parameter type of the method called by the delegate,
The following are examples of use:
Func<int, bool> myFunc = null;//all variables
MyFunc = x = CheckIsInt32 (x);
A lambda expression is used where the delegate encapsulates the method
private bool CheckIsInt32 (int pars)//encapsulated method
{
return pars = = 5;
}
bool OK = MyFunc (5);//Invoke Delegate
msdn:http://msdn.microsoft.com/zh-cn/library/bb534303 (vs.95). aspx
But what if we need to encapsulate a method that does not return a value? Just use action!.
can use
Action<t1, T2, T3, t4> delegates pass the method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature defined by this delegate. That is, the encapsulated method must have four parameters that are passed to it by value and cannot return a value. (in C #, the method must return void.) In Visual Basic, you must pass a Sub ... END SUB structure to define it. Typically, this method is used to perform an operation.
Use the same method as Func!
msdn:http://msdn.microsoft.com/zh-cn/library/bb548654 (vs.95). aspx
Action: There is neither return nor parameter, using the following method:
Action
Action = null;//Define Action
Action = checkisvoid;//Encapsulation method, only the name of the method is required
Action ();//Call
Summarize:
Using func<t,tresult> and action<t>,action instead of delegate is all about simplifying the code, using less code to achieve the same effect, without requiring us to display a delegate for the Declaration.
The last parameter of func<t,tresult> is always the return type, while action<t> is not a return type, and the action is not returned type and parameter input.
Transferred from: http://blog.csdn.net/liuweitoo/article/details/8156802
Func and action