纵有疾风起,人生不言弃
对函数类型的封装,相当于函数指针
Action,Func
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
using System;delegate double Fun( double x );public class DelegateIntegral{ public static void Main() { Fun fun = new Fun(Math.Sin); double d = Integral( fun, 0, Math.PI/2, 1e-4 ); Console.WriteLine( d ); Fun fun2 = new Fun( Linear ); double d2 = Integral( fun2, 0, 2, 1e-3 ); Console.WriteLine( d2 ); Rnd rnd = new Rnd(); double d3 = Integral( new Fun(rnd.Num), 0, 1, 0.01 ); Console.WriteLine( d3 ); } static double Linear( double a ) { return a*2+1; } class Rnd { Random r = new Random(); public double Num( double x ) { return r.NextDouble(); } } static double Integral(Fun f, double a, double b, double eps)// 积分计算 { int n,k; double fa,fb,h,t1,p,s,x,t=0; fa=f(a); fb=f(b); // 迭代初值 n=1; h=b-a; t1=h*(fa+fb)/2.0; p=double.MaxValue; // 迭代计算 while (p>=eps) { s=0.0; for (k=0;k<=n-1;k++) { x=a+(k+0.5)*h; s=s+f(x); } t=(t1+h*s)/2.0; p=Math.Abs(t1-t); t1=t; n=n+n; h=h/2.0; } return t; }}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
using System;public delegate void DownloadStartHandler(object sender, DownloadStartEventArgs e); //声明委托public delegate void DownloadEndHandler(object sender, DownloadEndEventArgs e); public delegate void DownloadingHandler(object sender, DownloadingEventArgs e); public class DownloadStartEventArgs{ public string Url{ get{ return _url;} set{ _url=value;} } private string _url; public DownloadStartEventArgs( string url ){ this._url = url; }}public class DownloadEndEventArgs{ public string Url{ get{ return _url;} set{ _url=value;} } private string _url; public long ByteCount { get { return _byteCount; } set{ _byteCount=value;} } private long _byteCount; public DownloadEndEventArgs( string url, long size ){ this._url = url; this._byteCount=size; }}public class DownloadingEventArgs{ public string Url{ get{ return _url;} set{ _url=value;} } private string _url; public double Percent { get { return _percent; } set{ _percent=value;} } private double _percent; public DownloadingEventArgs( string url, double percent ){ this._url = url; this._percent=percent; }}public class Crawler{ public event DownloadStartHandler DownloadStart; // 声明事件 public event DownloadEndHandler DownloadEnd; // 声明事件 public event DownloadingHandler Downloading; // 声明事件 public string Name { get{return name;} set{ name=value;} } private string name; private string site; public Crawler( string name, string site ) { this.name = name; this.site = site; } public void Craw() { while( true ) { string url = GetNextUrl(); if( url == null ) break; long size = GetSizeOfUrl( url ); if( DownloadStart != null ) //下载开始的事件发生 { DownloadStart( this, new DownloadStartEventArgs(url)); } for( long i=0; i<size+1024; i+=1024 ) { //下载数据。。。 System.Threading.Thread.Sleep( 100 ); double percent = (int)(i*100.0/size); if( percent>100) percent=100; if( Downloading != null ) //下载数据的事件发生 { Downloading( this, new DownloadingEventArgs(url, percent)); } } if( DownloadEnd != null ) //下载结束的事件发生 { DownloadEnd( this, new DownloadEndEventArgs(url, size)); } } } private string GetNextUrl() { int a = rnd.Next(10); if( a == 0 ) return null; return site + "/Page" + a + ".htm"; } private long GetSizeOfUrl( string url) { return rnd.Next(3000 * url.Length); } private Random rnd = new Random();}class Test{ static void Main() { Crawler crawler = new Crawler("Crawer101", "http://www.pku.edu.cn"); crawler.DownloadStart += new DownloadStartHandler( ShowStart ); //注册事件 crawler.DownloadEnd += new DownloadEndHandler( ShowEnd ); crawler.Downloading += new DownloadingHandler( ShowPercent ); crawler.Craw(); } static void ShowStart(object sender, DownloadStartEventArgs e){ Console.WriteLine( (sender as Crawler).Name + "开始下载" + e.Url ); } static void ShowEnd(object sender, DownloadEndEventArgs e){ Console.WriteLine( "\n\r下载" + e.Url + "结束,其下载" + e.ByteCount + "字节" ); } static void ShowPercent(object sender, DownloadingEventArgs e){ Console.Write( "\r下载" + e.Url + "......." + e.Percent + "%" ); }}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
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); } } }}