匿名錶達式,lambda表達式,Linq的示例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.   
  10. using System.Threading;
  11.   
  12. namespace MethodDelegateLamda
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.   
  21.   
  22.         //示例1,使用線程
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             //csharp 1.0
  26.             //使用委託,使用已定義好的函數
  27.             new Thread(new ThreadStart(MyFun)).Start();
  28.   
  29.             //csharp 2.0
  30.             //省略委託:MyFun自動實例化爲ThreadStart委託(
  31.             new Thread(MyFun).Start();
  32.             //匿名方法
  33.             new Thread(new ThreadStart( delegate(){ Console.Write("my function"); })).Start();
  34.             //匿名方法,省略參數列表
  35.             new Thread(new ThreadStart( delegate{ Console.Write("my function"); })).Start();
  36.             //匿名方法,自動轉委託
  37.             new Thread( delegate(){ Console.Write("my function"); }).Start(); 
  38.   
  39.             //csharp 3.0
  40.             //Lambda表達式
  41.             new Thread(() => { Console.Write("my function"); }).Start();
  42.   
  43.         }
  44.   
  45.         void MyFun()
  46.         {
  47.             Console.Write("my function");
  48.         }
  49.   
  50.   
  51.         //示例2,使用事件
  52.   
  53.         private void button3_Click(object sender, EventArgs e)
  54.         {
  55.             Example3();
  56.         }
  57.         void Example3()
  58.         {
  59.             //csharp 1.0
  60.             //使用委託,使用自定義函數
  61.             this.MouseMove += new MouseEventHandler(Form1_MouseMove);
  62.   
  63.             //csharp 2.0
  64.             //自動轉委託
  65.             this.MouseMove += Form1_MouseMove; 
  66.   
  67.             Label lbl = this.label1; //這個變量下面使用了閉包(簡單地說,使用外部的局部變量)
  68.             this.MouseMove += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { lbl.Text = e.X + "," + e.Y; });
  69.             this.MouseMove += delegate(object sender, MouseEventArgs e) { lbl.Text = e.X + "," + e.Y; };
  70.   
  71.             //csharp 3.0 
  72.             //使用Lambda表達式
  73.             this.MouseMove += (object sender, MouseEventArgs e) => { lbl.Text = e.X + "," + e.Y; }; //Lamda
  74.             this.MouseMove += (sender, e) => { lbl.Text = e.X + "," + e.Y; }; //可以省略類型
  75.         }
  76.   
  77.         void Form1_MouseMove(object sender, MouseEventArgs e)
  78.         {
  79.             this.label1.Text = e.X + "," + e.Y;
  80.         }
  81.   
  82.         //示例3, 數組排序
  83.         class Book
  84.         {
  85.             public string title;
  86.             public double price;
  87.             public Book(string title, double price) { this.title=title; this.price=price; }
  88.         }
  89.         private void button2_Click(object sender, EventArgs e)
  90.         {
  91.             Random rnd = new Random();
  92.             Book [] books  = new Book[ 10];
  93.             forint i=0; i<books.Length; i++ ) books[i] = new Book( "Book"+i, rnd.Next(100) );
  94.   
  95.             //csharp 1.0
  96.             Array.Sort(books, new MyComparer()); //用一個IComparer
  97.   
  98.             //csharp 2.0
  99.             Array.Sort<Book>(books, new Comparison<Book>(delegate(Book book1, Book book2) { return (int)(book1.price - book2.price); })); //使用Comparison委託
  100.             Array.Sort<Book>(books, delegate(Book book1, Book book2) { return (int)(book1.price - book2.price); });
  101.   
  102.             //csharp 3.0
  103.             Array.Sort<Book>(books, (Book book1, Book book2) => (int)(book1.price - book2.price));
  104.             Array.Sort<Book>(books, (book1, book2) => (int)(book1.price - book2.price)); //省略參數類型
  105.   
  106.             //使用Linq
  107.             IOrderedEnumerable<Book> result = from book in books orderby book.price select book;
  108.   
  109.             var result2 = from book in books  where book.price>=0 orderby book.price select book.title;
  110.             foreach (string in result2) Console.Write(s);
  111.   
  112.             var result3 = books
  113.                 .Where<Book>(b => b.price>=0)
  114.                 .OrderBy<Book, double>(b => b.price, Comparer<double>.Default)
  115.                 .Select<Book,Book>(book => book);
  116.             foreach (Book b in result3) Console.Write(b.price+" ");
  117.   
  118.   
  119.   
  120.   
  121.        }
  122.   
  123.         class MyComparer : System.Collections.IComparer
  124.         {
  125.             public int Compare(object x1, object x2)
  126.             {
  127.                 return (int)(((Book)x1).price - ((Book)x2).price);
  128.             }
  129.         }
  130.   
  131.   
  132.   
  133.   
  134.   
  135.   
  136.     }
  137. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章