This documentation is archived and is not being maintained.


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]);


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.
The result type, which matches the return type of the method.
The delegate name.
formal-parameters (Optional)
Parameter list. If a parameter is a pointer, the delegate must be declared with the unsafe modifier.


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)
   public static void DelegateFunction(int i)
      System.Console.WriteLine("Called by delegate with number: {0}.", i);


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);

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


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

See Also

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