About Action<t>, func<t>, eventhandler<t>, event, delegate

Source: Internet
Author: User

C # initially had only delegate, and later versions encapsulated action<t>, func<t>, eventhandler<t>

About Action<t>

Actually, action<t> is equivalent to the old version.

  Public Delegate voidAction ();  Public Delegate voidAction<t1>(T1 arg1);  Public Delegate voidACTION&LT;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);

New Version One:

/// <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"); }

New version 2 (lambda expression)

/// <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 (); }

New version 3 (with parameters)

/// <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.

About Func<t>

Actually, func<t> is equivalent to the old version.

  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); }        }

Multi-parameter new wording:

/// <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 (); }

About Eventhandler<t>

Actually, eventhandler<t> is equivalent to the old version.

 Public Delegate void Customeventhandler (object  sender, CustomEventArgs a);  Public Event Customeventhandler raisecustomevent;

New wording:

 Public Event Eventhandler<customeventargs> raisecustomevent;

1:eventhandler is actually a special delegate, it is made up of. NET is a predefined delegate, and its form is fixed.
2: When using EventHandler, the return value of the handler function must be of type void, while using deleagate does not have this restriction.
3:delegate is equivalent to a pointer to a function that is used to bind the function return value and the parameter list to conform to the requirements of the delegate declaration.

Reference: http://www.cnblogs.com/cglNet/p/3395954.html

About Action<t>, func<t>, eventhandler<t>, event, delegate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.