C#進階特性_Lambda

來源:互聯網
上載者:User

標籤:

“Lambda 運算式”(lambda expression)是一個匿名函數

省略delegate,甚至省略參數類型;

直接用(參數)=> {語句或運算式}

 

例如:

button1.Click += (sender, e) =>{......}new Thread  ( () => {......} ). Start();PlotFun ( x => x*x, 0, 100 );

 

lambda特點:

lambda運算式比匿名函數簡單,不寫參數的匿名函數可以轉化成任意多個參數的委託。

 

使用樣本:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;  using System.Threading;  namespace MethodDelegateLamda{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }            //樣本1,使用線程        private void button1_Click(object sender, EventArgs e)        {            //csharp 1.0            //使用委託,使用已定義好的函數            new Thread(new ThreadStart(MyFun)).Start();              //csharp 2.0            //省略委託:MyFun自動執行個體化為ThreadStart委託(            new Thread(MyFun).Start();            //匿名方法            new Thread(new ThreadStart( delegate(){ Console.Write("my function"); })).Start();            //匿名方法,省略參數列表            new Thread(new ThreadStart( delegate{ Console.Write("my function"); })).Start();            //匿名方法,自動轉委託            new Thread( delegate(){ Console.Write("my function"); }).Start();               //csharp 3.0            //Lambda運算式            new Thread(() => { Console.Write("my function"); }).Start();          }          void MyFun()        {            Console.Write("my function");        }            //樣本2,使用事件          private void button3_Click(object sender, EventArgs e)        {            Example3();        }        void Example3()        {            //csharp 1.0            //使用委託,使用自訂函數            this.MouseMove += new MouseEventHandler(Form1_MouseMove);              //csharp 2.0            //自動轉委託            this.MouseMove += Form1_MouseMove;               Label lbl = this.label1; //這個變數下面使用了閉包(簡單地說,使用外部的局部變數)            this.MouseMove += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { lbl.Text = e.X + "," + e.Y; });            this.MouseMove += delegate(object sender, MouseEventArgs e) { lbl.Text = e.X + "," + e.Y; };              //csharp 3.0             //使用Lambda運算式            this.MouseMove += (object sender, MouseEventArgs e) => { lbl.Text = e.X + "," + e.Y; }; //Lamda            this.MouseMove += (sender, e) => { lbl.Text = e.X + "," + e.Y; }; //可以省略類型        }          void Form1_MouseMove(object sender, MouseEventArgs e)        {            this.label1.Text = e.X + "," + e.Y;        }          //樣本3, 數組排序        class Book        {            public string title;            public double price;            public Book(string title, double price) { this.title=title; this.price=price; }        }        private void button2_Click(object sender, EventArgs e)        {            Random rnd = new Random();            Book [] books  = new Book[ 10];            for( int i=0; i<books.Length; i++ ) books[i] = new Book( "Book"+i, rnd.Next(100) );              //csharp 1.0            Array.Sort(books, new MyComparer()); //用一個IComparer              //csharp 2.0            Array.Sort<Book>(books, new Comparison<Book>(delegate(Book book1, Book book2) { return (int)(book1.price - book2.price); })); //使用Comparison委託            Array.Sort<Book>(books, delegate(Book book1, Book book2) { return (int)(book1.price - book2.price); });              //csharp 3.0            Array.Sort<Book>(books, (Book book1, Book book2) => (int)(book1.price - book2.price));            Array.Sort<Book>(books, (book1, book2) => (int)(book1.price - book2.price)); //省略參數類型              //使用Linq            IOrderedEnumerable<Book> result = from book in books orderby book.price select book;              var result2 = from book in books  where book.price>=0 orderby book.price select book.title;            foreach (string s in result2) Console.Write(s);              var result3 = books                .Where<Book>(b => b.price>=0)                .OrderBy<Book, double>(b => b.price, Comparer<double>.Default)                .Select<Book,Book>(book => book);            foreach (Book b in result3) Console.Write(b.price+" ");       }          class MyComparer : System.Collections.IComparer        {            public int Compare(object x1, object x2)            {                return (int)(((Book)x1).price - ((Book)x2).price);            }        }    }}

 

C#進階特性_Lambda

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.