27 August 2013

Delegate

A delegate is simply a variable pointing to a method (or a list of methods)

Delegate type in .Net is type safe object that points to a method(or list of methods) that can be invoked whenever you want.
Basically delegate has to contain three elements

  • The name (or Address) of method which it is calling
  • Arguments, if method contains any
  • Return value, if method returns any

You can invoke method synchronously or asynchronously by using delegate. Delegate uses three methods to invoke any method. Those are Invoke(), BeginInvoke() and EndInvoke(). Invoke() is used to invoke the method synchronously. BeginInvoke() and EndInvoke() methods are used to invoke the method asynchronously.

Creating Simple Delegate:
If you want to create a delegate in C#, you have to use “delegate” keyword. You can give any name to your delegate, but signature (parameters, return type) has to match with the method.

For example if you want to create simple delegate that match to Add() method which takes two integer parameters and return type is string.

public delegate string SimpleDelMath(int i, int j);

As shown above we are creating the simple delegate in C# which has same signature as compared with Add() method.

namespace DelegatesinCSharp
{
    class SimpleDelegate
    {
        public static string Add(int x, int y)
        {
            return "Sum of " + x + " and " + y + " is : " + (x + y);
        }
    }
}

Invoke the Add() method by using SimpleDelMath delegate as shown below.

namespace DelegatesinCSharp
{
    class Program
    {
        public delegate string SimpleDelMath(int i, int j);       
 
        static void Main(string[] args)
        {
            SimpleDelMath objDel = new SimpleDelMath(SimpleDelegate.Add);
            Console.WriteLine(objDel(1, 2));
            Console.ReadLine();          
        }
    }
}

First we created the object for delegate SimpleDelMath, then we are invoking the method by passing the required parameters to delegate object as shown below.

objDel(1, 2);

Multicast Delegate in C#:
Multicast Delegate in C# means ability to invoke multiple methods by using delegate. To invoke multiple methods by using delegate, methods have to follow some rules.

For Multicast Delegate in C#, methods does not have return type and all methods need to have same signature.

namespace DelegatesinCSharp
{
    class MulticastDelegate
    {
        public static void Add(int x, int y)
        {
            Console.WriteLine("Sum of " + x + " and " + y + " is : " + (x + y));
        }

        public static void sub(int x, int y)
        {
            Console.WriteLine( "Sub of " + x + " and " + y + " is : " + (x - y));
        }
    }
}

namespace DelegatesinCSharp
{
    class Program
    {
        public delegate void MultiDelMath(int i, int j);
        static void Main(string[] args)
        {
            MultiDelMath objMultiDel = new MultiDelMath(MulticastDelegate.Add);
            //Adding another method to the delegate object
            objMultiDel += new MultiDelMath(MulticastDelegate.sub);
            objMultiDel(1, 2);
            Console.ReadLine();
        }
    }
}

As shown above, created the delegate object MultiDelMath. At first added the Add() method to MultiDelMath delegate and next added the Sub() method to MultiDelMath delegate b y using += operator as shown below.

            MultiDelMath objMultiDel = new MultiDelMath(MulticastDelegate.Add);
            //Adding another method to the delegate object
            objMultiDel += new MultiDelMath(MulticastDelegate.sub);  

And we are invoking these two methods at a time by passing parameters as shown below.

            objMultiDel(1, 2); 

The output of multicast delegate is as shown below.




If you want, even you can delete method from delegate at any time as shown below. 

namespace DelegatesinCSharp
{
    class Program
    {  
        public delegate void MultiDelMath(int i, int j);
        static void Main(string[] args)
        {
            MultiDelMath objMultiDel = new MultiDelMath(MulticastDelegate.Add);
            //Adding another method to the delegate object
            objMultiDel += new MultiDelMath(MulticastDelegate.sub);
            objMultiDel(1, 2);
            //removing sub method from delegate object
            objMultiDel -= new MultiDelMath(MulticastDelegate.sub);
            objMultiDel(1, 2);
            Console.ReadLine();
        }
    }

As shown above, you are deleteing method sub by using below code. 

objMultiDel -= new MultiDelMath(MulticastDelegate.sub); 

The output after deleting the second method is as shown below, only invoke first method Add().