- Action
- Action<t>
- Func
- Func<t>
Action: encapsulates a method that does not have parameters and does not return a value
public delegate void Action ()
The action<t>:action generic implements the definition of 1 to 16 incoming parameters, but still 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 void Action<in t> (T obj) ... public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, In T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in t16> (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 arg14,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 (); }}
View Code
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 (); }}
View Code
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 delegate TResult Func<out tresult> ()
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,Out TResult>(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:
usingSystem;usingSystem.IO;Delegate BOOLWritemethod (); Public classtestdelegate{ Public Static voidMain () {outputtarget output=NewOutputtarget (); Writemethod Methodcall=output. Sendtofile; if(Methodcall ()) Console.WriteLine ("success!"); ElseConsole.WriteLine ("File write operation failed."); }} Public classoutputtarget{ Public BOOLSendtofile () {Try { stringfn =Path.gettempfilename (); StreamWriter SW=NewStreamWriter (FN); Sw. WriteLine ("Hello, world!."); Sw. Close (); return true; } Catch { return false; } }}
View Code
Now use Func directly:
usingSystem;usingSystem.IO; Public classtestdelegate{ Public Static voidMain () {outputtarget output=NewOutputtarget (); Func<BOOL> Methodcall =output. Sendtofile; //func<bool> Methodcall = delegate () {return output. Sendtofile (); }; Use the Func<tresult> delegate with an anonymous method//func<bool> Methodcall = () = output. Sendtofile (); Assign a lambda expression to func<t, tresult> delegate if(Methodcall ()) Console.WriteLine ("success!"); ElseConsole.WriteLine ("File write operation failed."); }} Public classoutputtarget{ Public BOOLSendtofile () {Try { stringfn =Path.gettempfilename (); StreamWriter SW=NewStreamWriter (FN); Sw. WriteLine ("Hello, world!."); Sw. Close (); return true; } Catch { return false; } }}
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