Transfer from http://www.cnblogs.com/LittleFeiHu/p/4035166.html
Action: Encapsulates a method that does not have parameters and does not return a value
Public Delegate voidAction () Action<T>: The generic for action implements the definition of 1 to 16 incoming parameters, but still has no return value, concludes that the action does not support the return value, and if a return value is required, use another system delegate func Public Delegate voidaction<inchT>(T obj) ... Public Delegate voidaction<inchT1,inchT2,inchT3,inchT4,inchT5,inchT6,inchT7,inchT8,inchT9,inchT10,inchT11,inchT12,inchT13,inchT14,inchT15,inchT16>(T1 arg1,T2 arg2,T3 arg3,T4 arg4,T5 arg5,T6 arg6,T7 arg7,T8 arg8,T9 arg9,T10 arg10,T11 arg11,T12 arg12,T13 arg13,T14 a rg14,t15 arg15,t16 arg16)
This shows that the definition of action is very simple, but such a delegate is too often used, if you define one at a time is also possible, many words will feel repetitive labor too much:
Custom delegates in the past:
usingSystem;usingSystem.Windows.Forms; Public Delegate voidShowvalue (); Public classname{Private stringinstancename; PublicName (stringname) { This. InstanceName =name; } Public voidDisplaytoconsole () {Console.WriteLine ( This. InstanceName); } Public voidDisplaytowindow () {MessageBox.Show ( This. InstanceName); }} Public classtesttestdelegate{ Public Static voidMain () {Name testname=NewName ("Koani"); Showvalue Showmethod=Testname.displaytowindow; Showmethod (); }}
Now use the action directly:
usingSystem;usingSystem.Windows.Forms; Public classname{Private stringinstancename; PublicName (stringname) { This. InstanceName =name; } Public voidDisplaytoconsole () {Console.WriteLine ( This. InstanceName); } Public voidDisplaytowindow () {MessageBox.Show ( This. InstanceName); }} Public classtesttestdelegate{ Public Static voidMain () {Name testname=NewName ("Koani"); Action Showmethod=Testname.displaytowindow; //to use an Action delegate with an anonymous method//Action Showmethod = Delegate () {Testname.displaytowindow ();}; //Assigning a lambda expression to an Action delegate instance//Action Showmethod = () = Testname.displaytowindow ();Showmethod (); }}
The use of action<t> is similar, but the definition of action<t> is quite special, it has a keyword in,in is used to do what, according to MSDN explanation:
Related concepts such as smaller or lesser derived types I recommend understanding C # Covariance and contravariance in depth
Func: Encapsulates a method that does not have a parameter but returns the type value specified by the TResult parameter
Public DelegateTResult func< outTresult>() Func<T>: The func generic also implements 1 to 16 incoming parameters and supports return values. Public DelegateTResult func<inchT outTresult>(T arg) ... Public Delegate voidfunc<inchT1,inchT2,inchT3,inchT4,inchT5,inchT6,inchT7,inchT8,inchT9,inchT10,inchT11,inchT12,inchT13,inchT14,inchT15,inchT16, outTresult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 Arg6, T7 Arg7, T8 Arg8, T9 arg9, T10 a RG10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16)
The definition of Func is equally straightforward, and the same as action is to simplify the code to make it easier for "customers" to use:
Custom delegates in the past:
View Code
Now use Func directly:
View Code
By defining the definition of func, you can see not only the In keyword figure, but also an out keyword, MSDN's explanation is as follows (out as the output parameter of the method is also used frequently):
Related concepts such as more derived or higher types I still recommend an in-depth understanding of C # Covariance and Contravariance
C # System delegate action and Func