Func<t> and action<t> Delegate generics introduction
After. Net 3.5, Microsoft introduced the func<t> and action<t> generic delegates. Further simplifies the definition of a delegate.
The main manifestations of action<t> are as follows:
Public Delegate voidAction (); Public Delegate voidAction<t1>(T1 arg1); Public Delegate voidACTION<T1, t2>(T1 arg1, T2 arg2); Public Delegate voidAction<t1, T2, t3>(T1 arg1, T2 arg2, T3 arg3); Public Delegate voidAction<t1, T2, T3, t4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4); Public Delegate voidAction<t1, T2, T3, T4, t5> (T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
It can be seen from the definition of action<t>. Action<t> is not worth the return. Applies to anything that does not return a worthwhile method. For example:
/// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {//Synchronous ExecutionAction action =NewAction (WriteLine); Action.invoke (); //Asynchronous ExecutionAction Actionasy =NewAction (WriteLine2); Actionasy.begininvoke (resual=>console.writeline ("end of asynchronous execution"),NULL); Console.read (); } Private Static voidWriteLine () {Console.WriteLine ("action Synchronous Execution Delegate"); } Private Static voidWriteLine2 () {Console.WriteLine ("action executes the delegate asynchronously"); }
If you call a lambda expression, you can be more concise, as you can write to the above code:
/// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {//synchronous execution with lambda expression instead of WriteLineAction action =NewAction (() =>console.writeline ("action Synchronous Execution Delegate")); Action.invoke (); //asynchronous execution with lambda expression instead of writeLine2Action Actionasy =NewAction (() =>console.writeline ("action executes the delegate asynchronously")); Actionasy.begininvoke (resual=>console.writeline ("end of asynchronous execution"),NULL); Console.read (); } Private Static voidWriteLine () {Console.WriteLine ("action Synchronous Execution Delegate"); } Private Static voidWriteLine2 () {Console.WriteLine ("action executes the delegate asynchronously"); }
You can do this if there are parameters that need to be passed in,action<t>, for example:
/// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {//synchronous execution passes in a parameteraction<string> Action =Newaction<string> ((a) =>console.writeline (string. Format ("the action synchronously executes the delegate, passing in the parameter: {0}", a))); Action.invoke ("Xiao Li"); //asynchronously executes an incoming two parameteraction<string,int> actionasy =Newaction<string,int> (A, b) =>console.writeline ("the action executes the delegate asynchronously, passing in the parameter: {0},{1}", A, b)); Actionasy.begininvoke ("Xiao Li", A, Resual=>console.writeline ("end of asynchronous execution"),NULL); Console.read (); }
In the preceding code, the string type that is defined synchronously must be guaranteed to pass in the parameter A is also a string. Although there is no type definition for a, the system defaults to the type defined in the pre-generic. Similarly, asynchronous delegates are the same. Otherwise it will be an error.
The main manifestations of func<t> are as follows:
Public DelegateTResult func<tresult>(); Public DelegateTResult Func<t1, tresult>(T1 arg1); Public DelegateTResult func<t1, T2, tresult>(T1 arg1, T2 arg2); Public DelegateTResult func<t1, T2, T3, tresult>(T1 arg1, T2 arg2, T3 arg3); Public DelegateTResult func<t1, T2, T3, T4, tresult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4); Public DelegateTResult func<t1, T2, T3, T4, T5, tresult> (T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
The definition of a func<t> delegate is relative to action<t>. Action<t> is a delegate that does not return a value that is worth the method delegate,func<t> is a return. The type of the return value, which is constrained by the type defined in the generic. For example:
/// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {//Asynchronous Executionfunc<string> funcasy =Newfunc<string> (() ={people tpeo=NewPeople ("asynchronous Xiao Li",Ten); returntpeo.tostring (); } ); Funcasy.begininvoke (resual= { //executes asynchronously, retrieving the returned result from the callback functionConsole.WriteLine (Funcasy.endinvoke (resual)); Console.WriteLine ("end of asynchronous execution"); }, NULL); //Synchronous Executionfunc<string> Func =Newfunc<string> (() ={people tpeo=NewPeople ("sync Xiao Li", A); returntpeo.tostring (); } ); //synchronous execution, get return resultsConsole.WriteLine (Func.invoke ()); Console.read (); } Public classpeople { Public stringName {Get;Set; } Public intAge {Get;Set; } PublicPeople (stringPName,intPAge) { This. Name =PName; This. Age =PAge; } Public Override stringToString () {return string. Format ("name {0}, age {1}", This. Name, This. Age); } }
The output results are as follows:
If you have parameters, you can write this:
/// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {//asynchronously executes a parameter that passes in a people type, returning the result of a sting typeFunc<people,string> funcasy =NewFunc<people,string> ((ppeople) = { returnPpeople.name; } ); Funcasy.begininvoke (NewPeople ("asynchronous Xiao Li", A), resual = { //executes asynchronously, retrieving the returned result from the callback functionConsole.WriteLine (Funcasy.endinvoke (resual)); Console.WriteLine ("end of asynchronous execution"); }, NULL); //synchronously executes a parameter that passes in a string,int type, returning the result of a people typefunc<string,int, people> Func =Newfunc<string,int, People> ((pname,page) ={people tpeo=Newpeople (PName, PAge); returnTpeo; } ); //synchronous execution, returning resultsConsole.WriteLine (Func.invoke ("sync Xiao Li", A). ToString ()); Console.read (); }
Article excerpt from: cglnet Source: http://www.cnblogs.com/cglnet
Func<t> and action<t> Delegate generics