HOW TO:宣告、產生和使用委派 (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 (含) 以後版本中,委派也可以使用匿名方法來宣告及初始化,如下列範例所示:

// 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# 程式設計手冊)

下列範例說明宣告、產生和使用委派。 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);
    

    這會建立與靜態方法 Test.PrintTitle 相關聯的新委派物件。 同樣地,也會傳遞在 totaller 物件上的非靜態 AddBookToTotal 方法,如下列範例所示:

    bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);
    

    在這兩種情況下,新的委派物件會傳遞至 ProcessPaperbackBooks 方法。

    在建立委派之後,與其關聯的方法永遠不會變更。委派物件是不可變的。

  • 呼叫委派。

    在建立委派物件之後,該委派物件通常是傳遞到將會呼叫委派的其他程式碼。 委派物件是使用委派物件的名稱,跟隨著傳遞給委派的括號引數來呼叫。 下列是委派呼叫的範例:

    processBook(b);
    

    如同這個範例,您可以同步呼叫委派,或是使用 BeginInvokeEndInvoke 方法進行非同步呼叫。

請參閱

參考

事件 (C# 程式設計手冊)

委派 (C# 程式設計手冊)

概念

C# 程式設計手冊