委托聲明定義一種類型
委托的一個有趣且有用的屬性是
此源碼下載
示例
示例
此外
委托和事件
委托與接口
示例
下面的示例闡釋聲明
委托的使用促進了書店數據庫和客戶代碼之間功能的良好分隔
// bookstore
using System;
// A set of classes for handling a bookstore:
namespace Bookstore
{
using System
// 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
{
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
{
list
}
// Call a passed
public void ProcessPaperbackBooks(ProcessBookDelegate processBook)
{
foreach (Book b in list)
{
if (b
// 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 =
decimal priceBooks =
internal void AddBookToTotal(Book book)
{
countBooks +=
priceBooks += book
}
internal decimal AveragePrice()
{
return priceBooks / countBooks;
}
}
// Class to test the book database:
class Test
{
// Print the title of the book
static void PrintTitle(Book b)
{
Console
}
// Execution starts here
static void Main()
{
BookDB bookDB = new BookDB()
// Initialize the database with some books:
AddBooks(bookDB)
// Print all the titles of paperbacks:
Console
// Create a new delegate object associated with the static
// method Test
bookDB
// 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
Console
totaller
}
// Initialize the book database with some test books:
static void AddBooks(BookDB bookDB)
{
bookDB
bookDB
bookDB
bookDB
}
}
}
輸出
Paperback Book Titles:
The C Programming Language
The Unicode Standard
Dogbert
Average Paperback Book Price: $
代碼討論
聲明委托 以下語句
public delegate void ProcessBookDelegate(Book book)
聲明一個新的委托類型
實例化委托 聲明了委托類型後
下列語句
bookDB
創建與靜態方法 Test
bookDB
ProcessBookDelegate(totaller
創建與對象 totaller 上的非靜態方法 AddBookToTotal 關聯的新的委托對象
請注意一旦創建了委托
調用委托 創建委托對象後
processBook(b)
示例
本示例演示組合委托
// compose
using System;
delegate void MyDelegate(string s)
class MyClass
{
public static void Hello(string s)
{
Console
}
public static void Goodbye(string s)
{
Console
}
public static void Main()
{
MyDelegate a
// Create the delegate object a that references
// the method Hello:
a = new MyDelegate(Hello)
// Create the delegate object b that references
// the method Goodbye:
b = new MyDelegate(Goodbye)
// The two delegates
// which calls both methods in order:
c = a + b;
// Remove a from the composed delegate
// which calls only the method Goodbye:
d = c
Console
a(
Console
b(
Console
c(
Console
d(
}
}
輸出
Invoking delegate a:
Hello
Invoking delegate b:
Goodbye
Invoking delegate c:
Hello
Goodbye
Invoking delegate d:
Goodbye
委托和事件
委托非常適合於用作事件(從一個組件就該組件中的更改通知
委托與接口
委托和接口的類似之處是
委托在以下情況下很有用
調用單個方法
一個類可能希望有方法規范的多個實現
希望允許使用靜態方法實現規范
希望類似事件的設計模式
調用方不需要知道或獲得在其上定義方法的對象
實現的提供程序希望只對少數選擇組件
需要方便的組合
接口在以下情況下很有用
規范定義將調用的一組相關方法
類通常只實現規范一次
接口的調用方希望轉換為接口類型或從接口類型轉換
From:http://tw.wingwit.com/Article/program/net/201311/12347.html