在 2.0 以前的 C# 版本中,聲明委託的惟一方法是使用命名方法。C# 2.0 引入了匿名方法,而在 C# 3.0 及更高版本中,Lambda 表達式取代了匿名方法,做爲編寫內聯代碼的首選方式html
// 聲明一個委託 delegate void Printer(string s); class TestClass { static void Main() { //lambda表達式 Printer pp = x => { Console.WriteLine("print:" + x); }; pp("sky");//匿名方法的語法:delegate(){//裏邊是代碼塊}
// 摘要: // Initializes a new instance of the System.Threading.Thread class. // // 參數: // start: // A System.Threading.ThreadStart delegate that represents the methods to be // invoked when this thread begins executing. // // 異常: // System.ArgumentNullException: // The start parameter is null. System.Threading.Thread t1 = new System.Threading.Thread (delegate() { System.Console.Write("Hello, "); System.Console.WriteLine("World!"); }); t1.Start(); // 匿名函數 Printer p = delegate(string j) { System.Console.WriteLine(j); }; // Results from the anonymous delegate call: p("The delegate using the anonymous method is called."); // 委託:將方法做爲方法的參數 //下邊將方法綁定到委託 //參考:張子陽的c#委託和事件http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html p = new Printer(TestClass.DoWork); p += TestClass.DoWork1; // p -= TestClass.DoWork; //p -= TestClass.DoWork1; // Results from the old style delegate call: p("The delegate using the named method is called."); } // The method associated with the named delegate: static void DoWork(string k) { System.Console.WriteLine(k); } static void DoWork1(string k) { System.Console.WriteLine(k); } }
//無參數的委託
// 聲明一個委託 delegate void del(); class TestClass { static void Main() { del myDelegate1 = () => { Console.WriteLine("00xx"); }; myDelegate1(); System.Threading.Thread t1 = new System.Threading.Thread (()=> { System.Console.Write("Hello, "); System.Console.WriteLine("World!"); }); t1.Start(); t1 = new System.Threading.Thread (delegate() { System.Console.Write("Hello, "); System.Console.WriteLine("World!"); }); t1.Start(); } }
MSDN上的委託例子:算法
下面的示例闡釋聲明、實例化和使用委託。BookDB 類封裝一個書店數據庫,它維護一個書籍數據庫。它公開 ProcessPaperbackBooks 方法,該方法在數據庫中查找全部平裝書,並對每本平裝書調用一個委託。使用的 delegate 類型名爲 ProcessBookDelegate。Test 類使用該類打印平裝書的書名和平均價格。數據庫
委託的使用促進了書店數據庫和客戶代碼之間功能的良好分隔。客戶代碼不知道書籍的存儲方式和書店代碼查找平裝書的方式。書店代碼也不知道找到平裝書後將對平裝書執行什麼處理。c#
// A set of classes for handling a bookstore: namespace Bookstore { using System.Collections; // Describes a book in the book list: public struct Book { public string Title; // Title of the book. public string Author; // Author of the book. public decimal Price; // Price of the book. public bool Paperback; // Is it paperback? public Book(string title, string author, decimal price, bool paperBack) { Title = title; Author = author; Price = price; Paperback = paperBack; } } // Declare a delegate type for processing a book: public delegate void ProcessBookDelegate(Book book); // Maintains a book database. public class BookDB { // List of all books in the database: ArrayList list = new ArrayList(); // Add a book to the database: public void AddBook(string title, string author, decimal price, bool paperBack) { list.Add(new Book(title, author, price, paperBack)); } // Call a passed-in delegate on each paperback book to process it: public void ProcessPaperbackBooks(ProcessBookDelegate processBook) { foreach (Book b in list) { if (b.Paperback) // Calling the delegate: processBook(b); } } } } // Using the Bookstore classes: namespace BookTestClient { using Bookstore; // Class to total and average prices of books: class PriceTotaller { int countBooks = 0; decimal priceBooks = 0.0m; internal void AddBookToTotal(Book book) { countBooks += 1; priceBooks += book.Price; } internal decimal AveragePrice() { return priceBooks / countBooks; } } // Class to test the book database: class TestBookDB { // Print the title of the book. static void PrintTitle(Book b) { System.Console.WriteLine(" {0}", b.Title); } // Execution starts here. static void Main() { BookDB bookDB = new BookDB(); // Initialize the database with some books: AddBooks(bookDB); // Print all the titles of paperbacks: System.Console.WriteLine("Paperback Book Titles:"); // Create a new delegate object associated with the static // method Test.PrintTitle: bookDB.ProcessPaperbackBooks(PrintTitle); // Get the average price of a paperback by using // a PriceTotaller object: PriceTotaller totaller = new PriceTotaller(); // Create a new delegate object associated with the nonstatic // method AddBookToTotal on the object totaller: bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal); System.Console.WriteLine("Average Paperback Book Price: ${0:#.##}", totaller.AveragePrice()); } // Initialize the book database with some test books: static void AddBooks(BookDB bookDB) { bookDB.AddBook("The C Programming Language", "Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true); bookDB.AddBook("The Unicode Standard 2.0", "The Unicode Consortium", 39.95m, true); bookDB.AddBook("The MS-DOS Encyclopedia", "Ray Duncan", 129.95m, false); bookDB.AddBook("Dogbert's Clues for the Clueless", "Scott Adams", 12.00m, true); } } }
輸出:設計模式
Paperback Book Titles: The C Programming Language The Unicode Standard 2.0 Dogbert's Clues for the Clueless Average Paperback Book Price: $23.97
聲明委託less
public delegate void ProcessBookDelegate(Book book);異步
實例化委託。函數
聲明瞭委託類型後,必須建立委託對象並使之與特定方法關聯。在上一個示例中,您經過按下面示例中的方式將 PrintTitle 方法傳遞到 ProcessPaperbackBooks方法來實現這一點:post
bookDB.ProcessPaperbackBooks(PrintTitle);this
這將建立與靜態方法 Test.PrintTitle 關聯的新委託對象。相似地,對象 totaller 的非靜態方法 AddBookToTotal 是按下面示例中的方式傳遞的:
bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);
在兩個示例中,都向 ProcessPaperbackBooks 方法傳遞了一個新的委託對象。
委託建立後,它的關聯方法就不能更改;委託對象是不可變的。
調用委託。
建立委託對象後,一般將委託對象傳遞給將調用該委託的其餘代碼。經過委託對象的名稱(後面跟着要傳遞給委託的參數,括在括號內)調用委託對象。下面是委託調用的示例:
processBook(b);
與本例同樣,能夠經過使用 BeginInvoke 和 EndInvoke 方法同步或異步調用委託。
在C#中使用線程的方法不少,使用委託的BeginInvoke和EndInvoke方法就是其中之一。BeginInvoke方法可使用線程異步地執行委託所指向的方法。而後經過EndInvoke方法得到方法的返回值(EndInvoke方法的返回值就是被調用方法的返回值),或是肯定方法已經被成功調用。咱們能夠經過四種方法從EndInvoke方法來得到返回值。
詳情:這裏
分析:
看起來 是否是很像接口啊 給後來着留一個委託 關注返回值和參數
後來者實現委託 內部能夠隨便定義東東 可是 返回值和參數必須和委託的同樣
後來者想咋實現就咋實現
什麼時候使用委託什麼時候使用接口呢 來自MSDN:
委託和接口都容許類設計器分離類型聲明和實現。任何類或結構都能繼承和實現給定的接口。能夠爲任何類上的方法建立委託,前提是該方法符合委託的方法簽名。接口引用或委託可由不瞭解實現該接口或委託方法的類的對象使用。既然存在這些類似性,那麼類設計器什麼時候應使用委託,什麼時候又該使用接口呢? 在如下狀況下,請使用委託: 當使用事件設計模式時。 當封裝靜態方法可取時。 當調用方不須要訪問實現該方法的對象中的其餘屬性、方法或接口時。 須要方便的組合。 當類可能須要該方法的多個實現時。 在如下狀況下,請使用接口: 當存在一組可能被調用的相關方法時。 當類只須要方法的單個實現時。 當使用接口的類想要將該接口強制轉換爲其餘接口或類類型時。 當正在實現的方法連接到類的類型或標識時:例如比較方法。 使用單一方法接口而不使用委託的一個很好的示例是 IComparable 或泛型版本 IComparable<(Of <(T>)>)。IComparable 聲明 CompareTo 方法,該方法返回一個整數,指定相同類型的兩個對象之間的小於、等於或大於關係。IComparable 可用做排序算法的基礎。雖然將委託比較方法用做排序算法的基礎是有效的,可是並不理想。由於進行比較的能力屬於類,而比較算法不會在運行時改變,因此單一方法接口是理想的