C# 中的委托类似于 C 或 C++ 中的函数指针。使用委托使程序员可以将方法引用封装在委托对象内。然后可以将该委托对象传递给
可调用所引用方法的代码,而不必在编译时知道将调用哪个方法。(参见下例)
委托是一种用来引用静态方法或者对象实例方法的数据类型,与C 或 C++ 中的函数指针不同,后者只能引用静态方法。
委托的使用
public delegate int MyDelegate(int i);
//代理的返回类型及参数必须与所指向的方法(function)的返回类型及参数相同
public Class C
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public int function(int i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
方法代码;
}
}
![](/Images/OutliningIndicators/None.gif)
MyDelegate d = new MyDelegate(C.function);
int intvalue = 1;
d(intvalue);
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
MSDN实例:
BookDB 类封装一个书店数据库,它维护一个书籍数据库。它公开 ProcessPaperbackBooks
方法,该方法在数据库中查找所有平装书,
并为每本书调用一个委托。所使用的 delegate 类型称为 ProcessBookDelegate
。Test
类使用该类输出平装书的书名和平均价格。
委托的使用促进了书店数据库和客户代码之间功能的良好分隔。客户代码不知道书籍的存储方式和书店代码查找平装书的方式。
书店代码也不知道找到平装书后将对平装书进行什么处理。
// bookstore.cs
using System;
![](/Images/OutliningIndicators/None.gif)
namespace Bookstore
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
using System.Collections;
![](/Images/OutliningIndicators/InBlock.gif)
public struct Book
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public string Title; public string Author;
public decimal Price; public bool Paperback;
public Book(string title, string author, decimal price, bool paperBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Title = title;
Author = author;
Price = price;
Paperback = paperBack;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public delegate void ProcessBookDelegate(Book book);
![](/Images/OutliningIndicators/InBlock.gif)
// Maintains a book database.
public class BookDB
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ArrayList list = new ArrayList();
![](/Images/OutliningIndicators/InBlock.gif)
public void AddBook(string title, string author, decimal price, bool paperBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
list.Add(new Book(title, author, price, paperBack));
}
![](/Images/OutliningIndicators/InBlock.gif)
//委托发挥作用,委托作为参数传入方法ProcessPaperbackBooks内,该方法并不知道委托指 向的是什么方法
public void ProcessPaperbackBooks(ProcessBookDelegate processBook)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (Book b in list)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (b.Paperback)
// 调用委托所指向的方法 processBook(b);
}
}
}
}
![](/Images/OutliningIndicators/None.gif)
// Using the Bookstore classes:
namespace BookTestClient
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
using Bookstore;
![](/Images/OutliningIndicators/InBlock.gif)
// Class to total and average prices of books:
class PriceTotaller
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int countBooks = 0;
decimal priceBooks = 0.0m;
![](/Images/OutliningIndicators/InBlock.gif)
internal void AddBookToTotal(Book book)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
countBooks += 1;
priceBooks += book.Price;
}
![](/Images/OutliningIndicators/InBlock.gif)
internal decimal AveragePrice()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return priceBooks / countBooks;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
// Class to test the book database:
class Test
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Print the title of the book.
static void PrintTitle(Book b)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(" {0}", b.Title);
}
![](/Images/OutliningIndicators/InBlock.gif)
// Execution starts here.
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BookDB bookDB = new BookDB();
![](/Images/OutliningIndicators/InBlock.gif)
// Initialize the database with some books:
AddBooks(bookDB);
![](/Images/OutliningIndicators/InBlock.gif)
// Print all the titles of paperbacks:
Console.WriteLine("Paperback Book Titles:"); // Create a new delegate object associated with the static
bookDB.ProcessPaperbackBooks(new ProcessBookDelegate(PrintTitle));
![](/Images/OutliningIndicators/InBlock.gif)
// 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(new ProcessBookDelegate(totaller.AddBookToTotal));
Console.WriteLine("Average Paperback Book Price: ${0:#.##}",
totaller.AveragePrice());
}
![](/Images/OutliningIndicators/InBlock.gif)
// Initialize the book database with some test books:
static void AddBooks(BookDB bookDB)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![](/Images/OutliningIndicators/None.gif)
声明委托 以下语句:
public delegate void ProcessBookDelegate(Book book);
声明一个新的委托类型。每个委托类型都描述参数的数目和类型,以及它可以封装的方法的返回值类型。
每当需要一组新的参数类型或新的返回值类型时,都必须声明一个新的委托类型。
实例化委托 声明了委托类型后,必须创建委托对象并使之与特定方法关联。与所有其他对象类似,新的委托对象用 new 表达式创建。
但是当创建委托时,传递给 new 表达式的参数很特殊:
它的编写类似于方法调用,但没有方法的参数。
下列语句:
bookDB.ProcessPaperbackBooks(new ProcessBookDelegate(PrintTitle));
创建与静态方法 Test.PrintTitle
关联的新的委托对象。下列语句:
bookDB.ProcessPaperbackBooks(new
ProcessBookDelegate(totaller.AddBookToTotal));
创建与对象 totaller
上的非静态方法 AddBookToTotal
关联的新的委托对象。在两个例子中,新的委托对象都立即传递给 ProcessPaperbackBooks
方法。
请注意一旦创建了委托,它所关联到的方法便永不改变:委托对象不可改变。
调用委托 创建委托对象后,通常将委托对象传递给将调用该委托的其他代码。
通过委托对象的名称(后面跟着要传递给委托的参数,括在括号内)调用委托对象。下面是委托调用的示例:
processBook(b);