Export (0) Print
Expand All
82 out of 116 rated this helpful - Rate this topic

delegate

A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.

This declaration takes the following form::

[attributes] [modifiers] delegate result-type identifier ([formal-parameters]);

where:

attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see 17. Attributes.
modifiers (Optional)
The allowed modifiers are new and the four access modifiers.
result-type
The result type, which matches the return type of the method.
identifier
The delegate name.
formal-parameters (Optional)
Parameter list. If a parameter is a pointer, the delegate must be declared with the unsafe modifier.

Remarks

A delegate lets you pass a function as a parameter. The type safety of delegates requires the function you pass as a delegate to have the same signature as the delegate declaration. See the Delegates Tutorial for more information on using delegates.

The Delegates Tutorial shows how to compose delegates, that is, create delegates from other delegates. A delegate that contains an out parameter cannot be composed.

Delegates are the basis for events.

For more information on delegates, see 15. Delegates.

Example 1

The following is a simple example of declaring and using a delegate.

// keyword_delegate.cs
// delegate declaration
delegate void MyDelegate(int i);

class Program
{
   public static void Main()
   {
      TakesADelegate(new MyDelegate(DelegateFunction));
   }

   public static void TakesADelegate(MyDelegate SomeFunction)
   {
      SomeFunction(21);
   }
   
   public static void DelegateFunction(int i)
   {
      System.Console.WriteLine("Called by delegate with number: {0}.", i);
   }
}

Output

Called by delegate with number: 21.

Example 2

In the following example, one delegate is mapped to both static and instance methods and returns specific information from each.

// keyword_delegate2.cs
// Calling both static and instance methods from delegates
using System;

// delegate declaration
delegate void MyDelegate();

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

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

public class MainClass 
{
   static public void Main() 
   {
      MyClass p = new MyClass();

      // Map the delegate to the instance method:
      MyDelegate d = new MyDelegate(p.InstanceMethod);
      d();

      // Map to the static method:
      d = new MyDelegate(MyClass.StaticMethod);
      d();
   }
}

Output

A message from the instance method.
A message from the static method.

See Also

C# Keywords | Reference Types | C. Grammar | Delegates Tutorial

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.