在 2.0 之前的 C# 版本中,聲明委托的唯一方法是使用命名方法。C# 2.0 引入了匿名方法,而在 C# 3.0 及更高版本中,Lambda 表達式取代了匿名方法,作為編寫內聯代碼的首選方式
// 聲明一個委托 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 類使用該類打印平裝書的書名和平均價格。
委托的使用促進了書店數據庫和客戶代碼之間功能的良好分隔。客戶代碼不知道書籍的存儲方式和書店代碼查找平裝書的方式。書店代碼也不知道找到平裝書后將對平裝書執行什么處理。
// 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
聲明委托
public delegate void ProcessBookDelegate(Book book);
實例化委托。
聲明了委托類型后,必須創建委托對象並使之與特定方法關聯。在上一個示例中,您通過按下面示例中的方式將 PrintTitle 方法傳遞到 ProcessPaperbackBooks方法來實現這一點:
bookDB.ProcessPaperbackBooks(PrintTitle);
這將創建與靜態方法 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 可用作排序算法的基礎。雖然將委托比較方法用作排序算法的基礎是有效的,但是並不理想。因為進行比較的能力屬於類,而比較算法不會在運行時改變,所以單一方法接口是理想的