Call us @ +919804009688/ 09830386818/ 9831709190
  Delegates  
 
 
 
   
  What is Delegates?

Delegates word meaning is "representative".
 
 


 
  The above picture would have explained to you the definition of the delegate in the best possible way.

 
  Eg: When a head of state dies, the President of US does not have time to attend the funeral personally. Instead, he dispatches a delegate. Often this delegate is the Vice President.

Very similar to this is the functionality of Delegates in .Net. A delegate holds a reference to other method. On occurrence of any particular cause/event- like Clicking a button - the delegate cause the appropriate function to fire. It actually receives the cause and then decides that in response which functions should get called. You can assign that which method should be called through the delegate when a particular event would fire.

A delegate in .Net is just like a function pointer in C#.
The most important property of a function pointer is that the syntax of the function and the pointer to the function must exactly be same. The returntypes and the list of arguments must match exactly.

Eg:

If a function looks like:                            int fact(int)

Then its function pointer should look like: int *p (int)    

Here, as you may see the return types and the list of arguments of both function and function pointer match exactly.

When we use delegate then there are 3 stages which we need to follow:

Declaration: delegate delegateName (--list of arguments--)

Object Creation: delegateName objDelegate = new delegateName(--function to which the delegate points--)

Invoking: objDelegate(--list of arguments that are to be passed to the function--)

A very basic example (SimpleDelegate1.cs):

using System;

namespace BasicDelegate
{
      // Declaration
      public delegate void SimpleDelegate();
      class TestDelegate
      {
           public static void MyFunc()
           {
                Console.WriteLine("I was called by delegate ...");
           }
           public static void Main()
           {
                // Instantiation
                SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
                // Invocation
                simpleDelegate();
           }
      }
}

 
 

The main uses of delegate comes in the following circumstances:

1) When we want to create the controls dynamically, then we cannot create the events for these controls beforehand as these controls are created only on runtime. In that case, if we want to bind these controls with their respective events then we need to use delegate.

So, delegate is first and foremost used for dynamic binding of controls with their events.

2) Delegate is also used in multitasking using threading.

Eg: Suppose we are copying a file from C: drive to D: drive. Then a progress bar would run which would indicate from time to time that how much contents have been copied - like 20% copied, 30% copied. This thing is an example of multiswitching, since here the thread is switching between 2 seperate opertaions. Once it is copying the data from one location to another location and then it is evaluating that how much content has been copied and increasing the progress bar accordingly. But, this switching is happening so fast, that the user gets a feeling as if 2 operations are happenng simultaneously.

So, in the above case, we may use a thread and make it switch between 2 operations. In this case also the delegate comes into play.

A detailed video tutorial has been provided on Delegate's usage for dynamic binding of controls with their events above: