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