Various delegates in. NET (Delegate, Action, Func)

Source: Internet
Author: User

1, Delegate, commissioned by the originator

        protected Delegate intClassdelegate (intXinty);//defining delegate types and Parameters        Static voidMain (string[] args) {classdelegate dele=NewClassdelegate (ADD);//instantiate a delegateConsole.WriteLine (Dele (1,2));//Invoke DelegateConsole.readkey (); }        Static intADD (intAintb) {returnA +b; }

2. Action, you can pass in parameters, no delegate with return value

Method 1, calling the method

        Static voidMain (string[] args) {Action<int,int> AC =Newaction<int,int> (Showaddresult);//instantiate a delegateAc1,2);//Invoke DelegateConsole.readkey (); }        Static voidShowaddresult (intAintb) {Console.WriteLine (a+b); }

Method 2, using a lambda expression

        Static void Main (string[] args)        {            Action<intint> AC = ((p, q) + = Console.WriteLine (p + q)); // instantiate a delegate            AC (12); // Invoke Delegate             Console.readkey ();        }

Method 3, as a parameter

        Static voidMain (string[] args) {Action<string> ac = (p = Console.WriteLine ("I am method 1, passing in the value:"+P));//instantiate a delegateaction<string> AC2 = (p = Console.WriteLine ("I am method 2, passing in the value:"+ p));//instantiate another delegateTest (AC,"parameter 1");//call the test method, passing in the delegate parameterTest (AC2,"parameter 1");//call the test method, passing in the delegate parameterConsole.readkey (); }        Static voidTest<t> (action<t>AC, T Inputparam)        {AC (Inputparam); }

3, Func, can pass in parameters, must have a return value of the delegate

Method 1, calling the method

        Static voidMain (string[] args) {Func<string> FC1 =Newfunc<string> (Showaddresult);//instantiate a delegate            stringresult = FC1 ();//Invoke DelegateConsole.WriteLine (Result);        Console.readkey (); }        Static stringShowaddresult () {return "the earth is round."; }

Method 2, using a lambda expression

        Static voidMain (string[] args) {            //instantiate a delegate, pay attention to the parentheses, write the value is the return value, cannot take returnfunc<string> FC1 = () ="the earth is round."; //instantiate another delegate, note that you can write multiple lines of code after the parentheses, but you must bring a returnfunc<string> FC2 = () =            {                return "the earth is round.";            }; stringresult = FC1 ();//Invoke Delegate            stringRESULT2 = FC2 ();//Invoke DelegateConsole.WriteLine (Result);            Console.WriteLine (RESULT2);        Console.readkey (); }

Method 3, as a parameter

        Static voidMain (string[] args) {            //instantiate a delegate, pay attention to the parentheses, write the value is the return value, cannot take returnfunc<int,string> FC1 = (p) = ="Incoming Parameters"+ P +", the earth is round."; //instantiate another delegate, note that you can write multiple lines of code after the parentheses, but you must bring a returnfunc<string,string> FC2 = (p) = =            {                return "Incoming Parameters"+ P +", the earth is round.";            }; stringresult = test<int> (FC1,1);//Invoke Delegate            stringRESULT2 = test<string> (FC2,"1");//Invoke DelegateConsole.WriteLine (Result);            Console.WriteLine (RESULT2);        Console.readkey (); }        Static stringTest<t> (Func<t,string>FC, T Inputparam) {            returnFC (Inputparam); }

Summarize:

Delegate at least 0 parameters, up to 32 parameters, can have no return value, or you can specify a return value type

Func can accept 0 to 16 incoming parameters and must have a return value

Action can accept 0 to 16 incoming parameters, no return value

Various delegates in. NET (Delegate, Action, Func)

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.