Partager via


Named Methods (C# Programming Guide) 

A delegate can be associated with a named method. When you instantiate a delegate 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 instanced method. Using named methods is the only way to instantiate a delegate in previous versions of C#. However, in a situation where creating a new method is undesirable overhead, C# 2.0 allows you to instantiate a delegate and specify a code block immediately that the delegate will process when called. These are called Anonymous Methods (C# Programming Guide).

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.

Although the delegate can use an out parameter, it is not recommended to use it with multicast event delegates because there is no way to know which delegate will be called.

Example 1

The following is a simple example of declaring and using a delegate. 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.
        System.Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
        for (int i = 1; i <= 5; i++)
        {
            d(i, 2);
        }
    }

    // Declare the associated method.
    void MultiplyNumbers(int m, double n)
    {
        System.Console.Write(m * n + " ");
    }
}

Output

Invoking the delegate using 'MultiplyNumbers':

2 4 6 8 10

Example 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()
    {
        System.Console.WriteLine("A message from the instance method.");
    }

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

class TestSampleClass
{
    static void Main()
    {
        SampleClass 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

Tasks

How to: Combine Delegates (Multicast Delegates)(C# Programming Guide)

Concepts

C# Programming Guide
Delegates (C# Programming Guide)
Events (C# Programming Guide)