News: - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

.Net C# Delegates

Started by sukishan, Jul 11, 2009, 03:15 PM

Previous topic - Next topic


.Net C# Delegates :

   Describes some basics about some of the great features of C# language namely Delegates and Events. These new constructs are used in object-oriented programming languages like C# and Java.

Delegates in C# .Net:
If we look at C++ there is a feature called callback function. This feature uses Pointers to Functions to pass them as parameters to other functions. Delegate is a similar feature but it is more type safe, which stands as a stark contrast with C++ function pointers. A delegate can hold reference/s to one more more functions and invoke them as and when needed.

A delegate needs the method's name and its parameters (input and output variables) when we create a delegate. But delegate is not a standalone construction. it's a class. Any delegate is inherited from base delegate class of .NET class library when it is declared. This can be from either of the two classes from System.Delegate or System.MulticastDelegate.

If the delegate contains a return type of void, then it is automatically aliased to the type of System.MulticastDelegate. This can support multiple functions with a += operator. If the delegate contains a non-void return type then it is aliased to System.Delegate class and it cannot support multiple methods.

Let us have a look at the following sample code.

class Figure
    public Figure(float a, float b, float c)
       m_xPos = a;
       m_yPos = b;
       m_zPos = c;
    public void InvertX()
        m_xPos = - m_xPos;

   public void InvertY()
     m_yPos = - m_yPos;

   public void InvertZ()
     m_zPos = - m_zPos;

   private float m_xPos = 0;
   private float m_yPos = 0;
   private float m_zPos = 0;


Now, we have a class named Figure and it has three private fields that use to store position and three methods to invert this position by every axis. In main class we declare delegate as follows:

public delegate void FigureDelegate();

And now in the main function we should use it like this:
Figure figure = new Figure(10,20,30);
FigureDelegate fx = new FigureDelegate(figure.InvertX);
FigureDelegate fy = new FigureDelegate(figure.InvertY);
FigureDelegate fz = new FigureDelegate(figure.InvertZ);
MulticastDelegate f_del = fx+fy+fz;

In this example we create three delegates of FigureDelegate type and attach to these elements our three methods from Figure class. Now every delegate keeps the address of the attached function. The last line of code is very interesting, here we create a delegate of base type (MulticastDelegate) and attach three of our already created delegates. As all our methods are of void return type they are automatically of type MutlticastDelegate and a MulticastDelegate can support multiple methods invocation also. Hence we can write

Figure figure = new Figure(10,20,30);
FigureDelegate fMulti = new FigureDelegate(figure.InvertX);
fMulti += new FigureDelegate(figure.InvertY);
A good beginning makes a good ending

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview