- 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);
- }
- }
- }
- }
匿名錶達式,lambda表達式,Linq的示例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.