匿名表达式,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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值