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

委派是一種類型,代表具有特定參數清單及傳回型別的方法參考。A delegate is a type that represents references to methods with a particular parameter list and return type. 當您具現化委派時,可以將其執行個體與任何具有相容簽章和傳回型別的方法產生關聯。When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. 您可以透過委派執行個體叫用 (或呼叫) 方法。You can invoke (or call) the method through the delegate instance.

委派可以用來將方法當做引數傳遞給其他方法。Delegates are used to pass methods as arguments to other methods. 事件處理常式就是透過委派叫用的方法。Event handlers are nothing more than methods that are invoked through delegates. 建立自訂方法後,像是 Windows 控制項這樣的類別就會在特定事件發生時呼叫您的方法。You create a custom method, and a class such as a windows control can call your method when a certain event occurs. 下列範例將示範委派宣告:The following example shows a delegate declaration:

public delegate int PerformCalculation(int x, int y);

來自符合委派類型之任何可存取類別或結構的任何方法都可以指派給委派。Any method from any accessible class or struct that matches the delegate type can be assigned to the delegate. 方法可以是靜態或執行個體方法。The method can be either static or an instance method. 如此即可用程式設計的方式變更方法呼叫,也可將新的程式碼插入現有的類別中。This makes it possible to programmatically change method calls, and also plug new code into existing classes.

注意

在方法多載的內容中,方法的簽章並不包括傳回值。In the context of method overloading, the signature of a method does not include the return value. 不過在委派的內容中,簽章卻包含傳回值。But in the context of delegates, the signature does include the return value. 換句話說,方法必須與委派擁有相同的傳回類型。In other words, a method must have the same return type as the delegate.

由於委派能夠將方法當做參數來參考,因此很適合用來定義回呼方法。This ability to refer to a method as a parameter makes delegates ideal for defining callback methods. 例如,可以將比較兩個物件的方法參考當成引數傳遞至排序演算法。For example, a reference to a method that compares two objects could be passed as an argument to a sort algorithm. 因為比較程式碼是在獨立的程序中,因此排序演算法可以用較普通的方式撰寫。Because the comparison code is in a separate procedure, the sort algorithm can be written in a more general way.

委派概觀Delegates Overview

委派包含下列屬性:Delegates have the following properties:

  • 委派與 C++ 函式指標相似,但委派完全為物件導向,而且不像 C++ 指標之於成員函式,會同時委派封裝物件執行個體與方法。Delegates are similar to C++ function pointers, but delegates are fully object-oriented, and unlike C++ pointers to member functions, delegates encapsulate both an object instance and a method.

  • 委派允許將方法當做參數傳遞。Delegates allow methods to be passed as parameters.

  • 委派可用於定義回呼方法。Delegates can be used to define callback methods.

  • 您可將委派鏈結在一起,例如,可在單一事件上呼叫多個方法。Delegates can be chained together; for example, multiple methods can be called on a single event.

  • 方法不需要完全符合委派類型。Methods do not have to match the delegate type exactly. 如需詳細資訊,請參閱在委派中使用差異For more information, see Using Variance in Delegates.

  • C# 2.0 版開始引進匿名方法的概念。此方法能夠以參數的方式傳遞程式碼區塊,取代了個別定義方法的作法。C# version 2.0 introduced the concept of Anonymous Methods, which allow code blocks to be passed as parameters in place of a separately defined method. C# 3.0 引進了 Lambda 運算式,做為更簡潔的內嵌 (Inline) 程式碼區塊撰寫方式。C# 3.0 introduced lambda expressions as a more concise way of writing inline code blocks. 匿名方法與 Lambda 運算式 (在特定內容中) 都會編譯為委派類型。Both anonymous methods and lambda expressions (in certain contexts) are compiled to delegate types. 現在,這些功能合稱為「匿名函式」(Anonymous Function)。Together, these features are now known as anonymous functions. 如需 Lambda 運算式的詳細資訊,請參閱匿名函式For more information about lambda expressions, see Anonymous Functions.

本節內容In This Section

C# 語言規格C# Language Specification

如需詳細資訊,請參閱 C# 語言規格中的委派For more information, see Delegates in the C# Language Specification. 語言規格是 C# 語法及用法的限定來源。The language specification is the definitive source for C# syntax and usage.

C# 3.0 Cookbook 第三版:250 個以上 C# 3.0 程式設計人員適用的方案中的委派、事件與 Lambda 運算式Delegates, Events, and Lambda Expressions in C# 3.0 Cookbook, Third Edition: More than 250 solutions for C# 3.0 programmers

了解 C# 3.0:掌握 C# 3.0 的基本概念中的委派與事件Delegates and Events in Learning C# 3.0: Master the fundamentals of C# 3.0

另請參閱See also