Shiman’s Weblog

Collecting stones from the sea-shore.

C# .NET Delegates : Define and Create

Define a Delegate

The delegate keyword is for defining new delegates. The delegate statement looks like a function signature. Rather, it defines a new delegate type. Function pointers matching the delegate signature and return type can be stored into the delegate. Therefore, only functions with similar signatures can be called through the delegate. This code defines a new delegate, which internally becomes a class named DelegateClass:

public delegate int DelegateClass(string info);

This is the syntax for defining a new delegate class:

  • accessibility delegate return delegatename(parameterslist)

Accessibility is limited to the valid accessibility of classes, such as public or private. The remainder of the syntax defines the signature and return type of the delegate. delegatename is the name of the delegate classification.

Because defining a delegate creates a new class, delegates can be defined anywhere a class is appropriate. A delegate can be defined in a namespace as a namespace member and within a class as a nested class, but not as a class field or a local variable within a method.

Create a Delegate

As a class, use the new keyword to create an instance of a delegate. Delegates are derived from the System.MulticastDelegate reference type. Multicast delegates are repositories of zero or more function pointers. The list of pointers in a multicast delegate is called the invocation list. When a delegate hosts multiple function pointers, the functions are called on a first-in, first-out (FIFO) basis.

The delegate constructor is not overloaded and has a single parameter, which is the target method. For an instance method, use the object.method format. If static, use the class.method format. If the method and delegate are contained in the same class, neither the object nor class name is required. The following code initializes a delegate in a variety of ways:

using System;

 

namespace Examples.Delegates

{

    public delegate void DelegateClass();

 

    public class Constructors

    {

        public static void Main()

        {

            DelegateClass del1 = new

                DelegateClass(Constructors.MethodA);

            DelegateClass del2 = new DelegateClass(MethodA);

 

            ZClass obj = new ZClass();

            DelegateClass del3 = new DelegateClass(obj.MethodB);           

        }

 

        public static void MethodA()

        {

        }

    }

 

    public class ZClass

    {

        public void MethodB()

        {

        }

    }

}

You can assign a function pointer directly to a delegate and omit the new operator, which is called delegate inference. Delegate inference infers a delegate signature from the function pointer, creates a new delegate, initializes the source delegate with the function pointer in the constructor, and assigns the source delegate to the target delegate. The source delegate and the target delegate should have compatible signatures. Although the code is more concise, the intent is not as obvious. Here is the previous code, changed for delegate inference:

DelegateClass del1 = Constructors.MethodA;

DelegateClass del2 = MethodA;

 

ZClass obj = new ZClass();

DelegateClass del3 = obj.MethodB;

The signatures of delegates are not entirely rigid. Through contravariance and covariance, there is some flexibility. The signature of the function pointer does not have to match the delegate signature exactly.

August 8, 2008 Posted by shiman | C#.Net, Computer Science, OOP, Programming | , , , , , , , , , | No Comments Yet