delegate, generic delegate, asynchronous delegate (new, problem please point out, some code is to take other bloggers)

Source: Internet
Author: User

Commissioned

is to put a method as a parameter into the declaration (Deletgate) delegate. To another method, or execute the delegate directly.

1     classDelegates2     {3          Public Delegate voidMyDelegate (stringmsg);4         Static voidMain (string[] args)5         {6MyDelegate myd =NewMyDelegate (father. Buytoys);7MyD"BBB");8 Console.read ();9         }Ten     } One      Public classFather A     { -          Public Static voidBuytoys (stringmsg) -         { the Console.WriteLine (msg); -Console.WriteLine ("AAA"); -         } -}

It can be seen that a delegate is declared first, and the Buytoys method in the Father class is placed as a parameter in the delegate when the instance is delegated. The delegate is then used. Output: BBB AAA.

Generic delegate

Action<t>: Delegate with no return value. Or return an empty delegate (that is, return directly)

Func<t,tresult>: The delegate must have a return value. T is a type that takes in parameters, Tresule is the type of the return parameter

Generic Delegate Detail Blog

A delegate is typically used to display a declaration delegate first.

 Public Delegate void mydelegate (string msg);

If you use a generic delegate, you do not have to display the declaration delegate.

     Public Delegate voidShowName ();  Public Delegate voidShownamewithparameter (stringname); ShowName ShowName=Person .    DisplayName;    ShowName (); Shownamewithparameter Shownamewithparameter=Person .    DisplayName; Shownamewithparameter (person.    INSTANCENAME); //above is the display delegate, the following is the generic delegateAction Actionshowname =Person .    DisplayName;    Actionshowname (); Action<string> actionShowName1 =Person .    DisplayName; ActionShowName1 (person. INSTANCENAME);
func<string, int> funreturnname = person. Getagebyname;
Console.WriteLine ("Age:{0}", Funreturnname (person. InstanceName));

Asynchronous delegate

The asynchronous delegate needs to use the BeginInvoke, which is the method that the BeginInvoke method uses to execute the delegate by using the thread asynchronously. The return value of the method is then obtained through the EndInvoke method.

        Private Static intNewTask (intms) {Console.WriteLine ("Task Start");            Thread.Sleep (MS); Random Random=NewRandom (); intn = random. Next (10000); Console.WriteLine ("Task Complete"); returnN; }        Private Delegate intNewtaskdelegate (intms); Static voidMain (string[] args) {            //newtaskdelegate task = NewTask;func<int,int> Task1 =NewTask; IAsyncResult AsyncResult= Task1. BeginInvoke ( -,NULL,NULL); //EndInvoke method will be blocked for 2 seconds            intresult =Task1.            EndInvoke (AsyncResult);            Console.WriteLine (result);        Console.read (); }

The above result is that the task begins, pauses for 2 seconds, gets the random number, displays the task completion, and finally, because EndInvoke returns a random number, displays the random number.

delegate, generic delegate, asynchronous delegate (new, problem please point out, some code is to take other bloggers)

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.