Paste the code below
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacelambda Expression {classProgram {Static voidMain (string[] args) { //the evolution of lambda expressions//The following is the code for creating a delegate instance in c#1.0func<string,int> delegatetest1 =Newfunc<string,int>(Callbackmethod); // //c#2.0 The use of anonymous methods to create a delegate instance, there is no need to go to the additional definition callback method Callbackmethodfunc<string,int> deledatetest2 =Delegate(stringtext) { returntext. Length; }; // //use Lamabda Expressions in C # 3.0 to create delegate instancesfunc<string,int> Delegatetest3 = (stringText) = =text. Length; // //You can omit the type parameter string, which simplifies the code againfunc<string,int> delegatetest4 = (text) = =text. Length; //parentheses omittedfunc<string,int> delegatetest = Text =text. Length; //Invoke DelegateConsole.WriteLine ("Use lambda to Retun text.length="+ DelegateTest ("HelloWorld")); Console.WriteLine ("------------------------------------------------------------"); //the above is the Lamada evolution process//The following is the use of Lamada in subscribing events//Define a buttonButton BTN =NewButton () {Text ="click on me." }; Btn. Click+ = (sender,e) =>reportevent ("Click Button Events", sender,e); //define the form displayForm form =NewForm () {Name ="forms created in the console", AutoSize =true, Controls ={BTN}}; //Run the formApplication.Run (form); Console.read (); } Private Static intCallbackmethod (stringtext) { returntext. Length; } Private Static voidReportEvent (stringTitleObjectsender, EventArgs e) {Console.WriteLine ("The event that occurred is: {0}", title); Console.WriteLine ("the object where the event occurred is: {0}", sender); Console.WriteLine ("the argument for the event occurred is: {0}", E.gettype ()); } }}
A different style, C # lambda expression