blog

Home / DeveloperSection / Blogs / Delegate

Delegate

Anchal Kesharwani 2777 12-Aug-2014

In this blog, I’m explaining the concept of delegate.

A delegate in C# is similar to a function pointer in C or C++. In C#, a delegate is type safe object that can point to another method (or multiple methods) that means a delegate can point to a method, which is having same signature as that of the delegate in the application, which can invoked at later time.

There are three steps in defining and using delegates:

1.       Declaration

2.       Instantiation

3.       Invocation

Declaring

delegate <return type><delegate-name><parameter list>

Consider the following example:

public delegate void DelegateExample (string s)

Example

using System;
namespace DelegateExample
{
    // Declaration of delegate
    publicdelegatevoidDelegateExample();
    classTest
    {
        publicstaticvoid MyMessage() // create a method for delegate
        {
            Console.WriteLine("I was called by delegate ...");
        }
        publicstaticvoid Main()
        {
            Test t = newTest();
            // Instantiation of delegate
            DelegateExample simpleDelegate = newDelegateExample(MyMessage); // It the function name
            // Invocation of delegate
            simpleDelegate(); // call this method
            Console.ReadKey();
        }
    }
}

Output is: I was called by delegate ...

In this example, how to a delegate declare, instantiate, and how to call.

Multicast Delegate

Multicast delegate means that they can point to more than one function at a time. Delegates objects can be composed using the “+” operator. Only delegates of the same type can be composed. The "-" operator can be used to remove a component delegate from a composed delegate.

Example

using System;
delegatevoidMulticastDelegateExample(int x, int y); //declare a multicast delegate
namespace MulticastDelegate
{
  
   classTestDelegate
   {
       publicstaticvoid AddNum(int p,int q)
       {
           Console.WriteLine("Add two numbers :" + (p + q));
       }
       publicstaticvoid MultNum(int p, int q)
       {
           Console.WriteLine("Multiply two numbers :" + (p + q));
       }
       staticvoid Main(string[] args)
       {
           //create delegate instances
           MulticastDelegateExample nc;
           MulticastDelegateExample nc1 = newMulticastDelegateExample(AddNum);
           MulticastDelegateExample nc2 = newMulticastDelegateExample(MultNum);
           nc = nc1;
           nc += nc2;
           //calling multicast both method together
           nc(1,2);
           nc -= newMulticastDelegateExample(AddNum);
           nc(2, 3); //calling only second method MultNum
         
           Console.ReadKey();
       }
   }
 
}

In this example, there are two methods and create two instances and one


reference of MulticastDelegateExample. Reference assign both instance


and call both method together and after only call second method.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By