委托(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 表达式,利用它们可以更简练地编写内联代码块。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. 这些功能现在统称为匿名函数。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 手册(第三版):面向 C# 3.0 程序员的超过 250 个解决方案中的委托、事件和 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