C # anonymous method delegate action delegate delegate delegate

Source: Internet
Author: User

Original address: Https://msdn.microsoft.com/zh-cn/library/bb882516.aspx

An anonymous function is an "inline" statement or expression that can be used anywhere a delegate type is required. You can use anonymous functions to initialize a named delegate, or to pass a named delegate (not a named delegate type) as a method parameter.

C # 2.0 introduces anonymous methods, whereas in C # 3.0 and later, LAMBDA expressions replace anonymous methods as the preferred way to write inline code.

Example reference:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Runtime.CompilerServices;5 usingSystem.Security.Cryptography.X509Certificates;6 usingSystem.Text;7 usingSystem.Threading.Tasks;8 9 namespaceconsoletestTen { One     Internal class Program A     { -         Private Static voidMain (string[] args) -         { the      //action Encapsulates a method that has only one parameter and does not return a value.
See here for more examples: https://msdn.microsoft.com/zh-cn/library/018hxwa8.aspx - varDD =Newaction<string> (Item) = - { - vars =string. Concat ("AA", item); + Console.Write (s.tostring ()); - }); + Add"BB"); at - Console.readkey (); - } - - - } in}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.CompilerServices;usingSystem.Security.Cryptography.X509Certificates;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoletest{Internal classProgram {Private Delegate BOOLDelegateage (intAge ); Private Static voidMain (string[] args) {            //lambda expression notationDelegateage delegateAge1 = (age) + Age > -; Delegateage DelegateAge2= (age) = =            {                returnAge > -;            }; Console.WriteLine (DelegateAge1 ( *)); Console.WriteLine (DelegateAge2 ( the));        Console.readkey (); }    }}

usingSystem;namespaceconsoletest{Internal classProgram {Private Delegate voidDel (); Private Static voidMain (string[] args) {            intn =0; //This can be played without parameters.Del d = () ={System.Console.WriteLine ("Copy #:{0}", ++N);            };            D ();        Console.readkey (); }    }}

usingSystem;usingSystem.IO;namespaceconsoletest{Internal classProgram {Private Delegate voidDel (intAintb); Private Static voidMain (string[] args) {            //multiple parameters in case you can play like thisDel d = (A, b) = ={System.Console.WriteLine ("a+b="+ (A +b).            ToString ());            }; //You can play it like that.Del D2 =Delegate(intAintb) {System.Console.WriteLine ("a+b="+ (A +b).            ToString ());            }; D (4,7); D2 (4,7);        Console.readkey (); }    }}

C # anonymous method delegate action delegate delegate 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.