"lambda expression" is an anonymous function
Omitting delegate, even omitting parameter types;
directly with (parameter) = = { statement or expression }
For example:
Button1. Click + = (sender, E) = ={...} New Thread (() =0 );
Lambda Features:
Lambda expressions are simpler than anonymous functions, and anonymous functions that do not write arguments can be converted to delegates of any number of arguments.
Examples of Use:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms; usingSystem.Threading; namespacemethoddelegatelamda{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } //Example 1, using Threads Private voidButton1_Click (Objectsender, EventArgs e) { //CSharp 1.0//Use a delegate to use a defined function NewThread (NewThreadStart (myfun)). Start (); //CSharp 2.0//Omit delegate: Myfun is automatically instantiated as a ThreadStart delegate ( NewThread (myfun). Start (); //Anonymous Methods NewThread (NewThreadStart (Delegate() {Console.Write ("my function"); })). Start (); //anonymous method, omitting argument list NewThread (NewThreadStart (Delegate{Console.Write ("my function"); })). Start (); //anonymous method, auto-delegate NewThread (Delegate() {Console.Write ("my function"); }). Start (); //CSharp 3.0//lambda expression NewThread (() = {Console.Write ("my function"); }). Start (); } voidmyfun () {Console.Write ("my function"); } //Example 2, using Events Private voidButton3_Click (Objectsender, EventArgs e) {Example3 (); } voidExample3 () {//CSharp 1.0//Use a delegate, use a custom function This. MouseMove + =NewMouseEventHandler (Form1_mousemove); //CSharp 2.0//Auto-Delegate This. MouseMove + =Form1_mousemove; Label LBL= This. Label1;//This variable uses a closure (in a nutshell, using an external local variable) This. MouseMove + =NewMouseEventHandler (Delegate(Objectsender, MouseEventArgs e) {lbl. Text = e.x +","+e.y;}); This. MouseMove + =Delegate(Objectsender, MouseEventArgs e) {lbl. Text = e.x +","+e.y;}; //CSharp 3.0//using lambda expressions This. MouseMove + = (Objectsender, MouseEventArgs e) = {lbl. Text = e.x +","+ e.y; };//Lamda This. MouseMove + = (sender, E) = = {LbL. Text = e.x +","+ e.y; };//type can be omitted } voidForm1_mousemove (Objectsender, MouseEventArgs e) { This. Label1. Text = e.x +","+e.y; } //Example 3, array sorting classBook { Public stringtitle; Public DoublePrice ; PublicBook (stringTitleDoublePrice) { This. Title=title; This. price=Price ;} } Private voidButton2_Click (Objectsender, EventArgs e) {Random rnd=NewRandom (); Book [] Books=Newbook[Ten]; for(intI=0; I<books. Length; i++) Books[i] =NewBook (" Book"+i, Rnd. Next ( -) ); //CSharp 1.0Array.Sort (Books,NewMycomparer ());//with a IComparer//CSharp 2.0Array.sort<book> (Books,NewComparison<book> (Delegate(Book Book1, book Book2) {return(int) (Book1.price-book2.price); }));//using the comparison delegateArray.sort<book> (Books,Delegate(Book Book1, book Book2) {return(int) (Book1.price-Book2.price); }); //CSharp 3.0Array.sort<book> (books, book Book1, book Book2) = (int) (Book1.price-book2.price)); Array.Sort<Book> (books, (Book1, book2) = (int) (Book1.price-book2.price));//omitting parameter types//using LINQiorderedenumerable<book> result = fromBookinchBooks byBook.priceSelectBook ; varRESULT2 = fromBookinchBookswherebook.price>=0 byBook.priceSelectBook.title; foreach(stringSinchresult2) Console.Write (s); varRESULT3 =Books. Where<Book> (b = b.price>=0) . by<book,Double> (b = b.price, comparer<Double>. Default). Select<Book,Book> (book =Book ); foreach(Book BinchRESULT3) Console.Write (b.price+" "); } classMyComparer:System.Collections.IComparer { Public intCompare (ObjectX1,Objectx2) { return(int) ((book) x1).(book) x2); } } }}
C # Advanced Features _lambda