Sunday, 25 March 2012

Delegate

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

Delegate Signatures
A delegate type is specified by a distinct signature, which includes a unique identifier, parameter list, and return type. The following code shows how to declare a delegate:
public delegate double UnitConversion(double from);
The example above contains a full definition of a delegate. Just like any other namespace element, delegates are declared as either public or internal, with a default of internal accessibility if the modifier is not specified. Delegates may also be declared as nested types, with accessibility modifiers allowable for their containing class or struct.
The C# keyword delegate identifies this as a delegate declaration. The return type of this delegate is double, its identifier is "UnitConversion," and it has a single parameter of type double. Looking at both the delegate identifier and parameter identifier, you can get an idea of the purpose of this delegate, which is to serve as a definition of methods that perform a conversion of units from one type to another.
A delegate declaration is appended with a semi-colon. Delegates do not have implementation. Instead, they are a skeleton defining the proper signature of delegate handler methods to which they may refer. The handler methods contain implementation that is executed when its referring delegate is invoked.
Example
using System;
namespace Delegates
{
    public delegate int DelegateToMethod(int x, int y);

    public class Math
    {
        public static int Add(int first, int second)
        {
            return first + second;
        }
        public static int Multiply(int first, int second)
        {
            return first * second;
        }

        public static int Divide(int first, int second)
        {
            return first / second;
        }
    }
    public class DelegateApp
    {
        public static void Main()
        {
            DelegateToMethod aDelegate = new DelegateToMethod(Math.Add);
            DelegateToMethod mDelegate = new DelegateToMethod(Math.Multiply);
            DelegateToMethod dDelegate = new DelegateToMethod(Math.Divide);
            Console.WriteLine("Calling the method Math.Add() through the aDelegate object");
            Console.WriteLine(aDelegate(5, 5));
            Console.WriteLine("Calling the method Math.Multiply() through the mDelegate object");
            Console.WriteLine(mDelegate(5, 5));
            Console.WriteLine("Calling the method Math.Divide() through the dDelegate object");
            Console.WriteLine(dDelegate(5, 5));
            Console.ReadLine();
        }
}
}
}


OUTPUT
Calling the method Math.Add() through the aDelegate object
10
Calling the method Math.Multiply() through the mDelegate object
25
Calling the method Math.Divide() through the dDelegate object
1


No comments:

Post a Comment