<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 如何:聲明、實例化和使用委托(C# 編程指南) 在 C# 1.0 及更高版本中,可以按以下示例所示聲明委托。 ``` // Declare a delegate. delegate void Del(string str); // Declare a method with the same signature as the delegate. static void Notify(string name) { Console.WriteLine("Notification received for: {0}", name); } ``` ``` // Create an instance of the delegate. Del del1 = new Del(Notify); ``` C# 2.0 提供了更簡單的方法來編寫上面的聲明,如以下示例所示。 ``` // C# 2.0 provides a simpler way to declare an instance of Del. Del del2 = Notify; ``` 在 C# 2.0 及更高版本中,還可以使用匿名方法來聲明和初始化[委托](https://msdn.microsoft.com/zh-cn/library/900fyy8e.aspx),如以下示例所示。 ``` // Instantiate Del by using an anonymous method. Del del3 = delegate(string name) { Console.WriteLine("Notification received for: {0}", name); }; ``` 在 C# 3.0 及更高版本中,還可以使用 Lambda 表達式來聲明和實例化委托,如以下示例所示。 ``` // Instantiate Del by using a lambda expression. Del del4 = name => { Console.WriteLine("Notification received for: {0}", name); }; ``` 有關更多信息,請參見 [Lambda 表達式(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397687.aspx)。 下面的示例闡釋聲明、實例化和使用委托。 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); } } } /* Output: 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); ``` 這將創建與[靜態](https://msdn.microsoft.com/zh-cn/library/98f28cdx.aspx)方法 Test.PrintTitle 關聯的新委托對象。類似地,對象 totaller 的非靜態方法 AddBookToTotal 是按下面示例中的方式傳遞的: ``` bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal); ``` 在兩個示例中,都向 ProcessPaperbackBooks 方法傳遞了一個新的委托對象。 委托創建后,它的關聯方法就不能更改;委托對象是不可變的。 * 調用委托。 創建委托對象后,通常將委托對象傳遞給將調用該委托的其他代碼。通過委托對象的名稱(后面跟著要傳遞給委托的參數,括在括號內)調用委托對象。下面是委托調用的示例: ``` processBook(b); ``` 與本例一樣,可以通過使用 **BeginInvoke** 和 **EndInvoke** 方法同步或異步調用委托。 ## 請參閱 [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) [事件(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/awbftdfh.aspx) [委托(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/ms173171.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看