分享方式:


如何宣告、具現化和使用委派 (C# 程式設計指南)

您可以使用下列任何方法來宣告委派:

  • 宣告委派類型,並宣告具有相符特徵標記的方法:
// Declare a delegate.
delegate void NotifyCallback(string str);

// Declare a method with the same signature as the delegate.
static void Notify(string name)
{
    Console.WriteLine($"Notification received for: {name}");
}
// Create an instance of the delegate.
NotifyCallback del1 = new NotifyCallback(Notify);
  • 將方法群組指派給委派類型:
// C# 2.0 provides a simpler way to declare an instance of NotifyCallback.
NotifyCallback del2 = Notify;
  • 宣告匿名方法:
// Instantiate NotifyCallback by using an anonymous method.
NotifyCallback del3 = delegate(string name)
    { Console.WriteLine($"Notification received for: {name}"); };
  • 使用 Lambda 運算式:
// Instantiate NotifyCallback by using a lambda expression.
NotifyCallback del4 = name =>  { Console.WriteLine($"Notification received for: {name}"); };

如需詳細資訊,請參閱 Lambda 運算式

下列範例說明如何宣告、具現化和使用委派。 BookDB 類別會封裝用來保留書籍資料的書店資料庫。 它會公開 ProcessPaperbackBooks 方法,此方法會尋找資料庫中的所有平裝書,並針對每本書呼叫委派。 所使用的 delegate 類型稱為 ProcessBookCallbackTest 類別會使用這個類別來列印平裝書的書名和平均價格。

使用委派,可在書店資料庫和用戶端程式碼之間建立良好的功能區隔。 用戶端程式碼不需要瞭解保存書籍的方式,或是書店程式碼尋找平裝書的方式。 書店程式碼不需要知道當它找到平裝書之後該如何處理它們。

範例

// 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 ProcessBookCallback(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(ProcessBookCallback 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 Test
    {
        // Print the title of the book.
        static void PrintTitle(Book b)
        {
            Console.WriteLine($"   {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:
            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);

            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 ProcessBookCallback(Book book);
    

    每個委派類型都會描述引數的數目和類型,以及它可以封裝之方法的傳回值類型。 每當需要一組新的引數類型或傳回值類型時,就必須宣告新的委派類型。

  • 具現化委派。

    宣告委派類型之後,就必須建立委派物件並將其關聯至特定的方法。 在上述範例中,您可以藉由將 PrintTitle 方法傳遞至 ProcessPaperbackBooks 方法來執行此動作,如下列範例所示:

    bookDB.ProcessPaperbackBooks(PrintTitle);
    

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

    bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);
    

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

    建立委派之後,與其相關聯的方法絕對不會變更;委派物件是不可變的。

  • 呼叫委派。

    建立委派物件之後,通常會將委派物件傳遞至其他將呼叫委派的程式碼。 委派物件的呼叫方法是,使用委派物件的名稱,後面接著要傳遞至委派且已加上括號的引數。 以下是委派呼叫的範例:

    processBook(b);
    

    委派可以同步呼叫 (如此範例所示),或使用 BeginInvokeEndInvoke 方法以非同步方式呼叫。

另請參閱