A different style, C # lambda expression

Source: Internet
Author: User

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

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.