带有命名方法的委托与匿名方法(C# 编程指南)Delegates with Named vs. Anonymous Methods (C# Programming Guide)

委托可以与命名方法相关联。A delegate can be associated with a named method. 使用命名方法实例化委托时,该方法作为参数传递,例如:When you instantiate a delegate by using a named method, the method is passed as a parameter, for example:

// Declare a delegate.
delegate void Del(int x);

// Define a named method.
void DoWork(int k) { /* ... */ }

// Instantiate the delegate using the method as a parameter.
Del d = obj.DoWork;

这称为使用命名方法。This is called using a named method. 使用命名方法构造的委托可以封装静态方法或实例方法。Delegates constructed with a named method can encapsulate either a static method or an instance method. 命名方法是在早期版本的 C# 中实例化委托的唯一方式。Named methods are the only way to instantiate a delegate in earlier versions of C#. 但是,如果创建新方法会造成多余开销,C# 允许你实例化委托并立即指定调用委托时委托将处理的代码块。However, in a situation where creating a new method is unwanted overhead, C# enables you to instantiate a delegate and immediately specify a code block that the delegate will process when it is called. 代码块可包含 Lambda 表达式或匿名方法。The block can contain either a lambda expression or an anonymous method. 有关详细信息,请参阅匿名函数For more information, see Anonymous Functions.

备注Remarks

作为委托参数传递的方法必须具有与委托声明相同的签名。The method that you pass as a delegate parameter must have the same signature as the delegate declaration.

委托实例可以封装静态方法或实例方法。A delegate instance may encapsulate either static or instance method.

尽管委托可以使用 out 参数,但不建议将该委托与多播事件委托配合使用,因为你无法知道将调用哪个委托。Although the delegate can use an out parameter, we do not recommend its use with multicast event delegates because you cannot know which delegate will be called.

示例 1Example 1

以下是声明和使用委托的简单示例。The following is a simple example of declaring and using a delegate. 请注意,委托 Del 与关联的方法 MultiplyNumbers 具有相同的签名Notice that both the delegate, Del, and the associated method, MultiplyNumbers, have the same signature

// Declare a delegate
delegate void Del(int i, double j);

class MathClass
{
    static void Main()
    {
        MathClass m = new MathClass();

        // Delegate instantiation using "MultiplyNumbers"
        Del d = m.MultiplyNumbers;

        // Invoke the delegate object.
        Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
        for (int i = 1; i <= 5; i++)
        {
            d(i, 2);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

    // Declare the associated method.
    void MultiplyNumbers(int m, double n)
    {
        Console.Write(m * n + " ");
    }
}
/* Output:
    Invoking the delegate using 'MultiplyNumbers':
    2 4 6 8 10
*/

示例 2Example 2

在下面的示例中,一个委托映射到静态方法和实例方法,并返回来自两种方法的具体信息。In the following example, one delegate is mapped to both static and instance methods and returns specific information from each.

// Declare a delegate
delegate void Del();

class SampleClass
{
    public void InstanceMethod()
    {
        Console.WriteLine("A message from the instance method.");
    }

    static public void StaticMethod()
    {
        Console.WriteLine("A message from the static method.");
    }
}

class TestSampleClass
{
    static void Main()
    {
        var sc = new SampleClass();

        // Map the delegate to the instance method:
        Del d = sc.InstanceMethod;
        d();

        // Map to the static method:
        d = SampleClass.StaticMethod;
        d();
    }
}
/* Output:
    A message from the instance method.
    A message from the static method.
*/

另请参阅See also