What is a delegate?

Answer Posted / vikul

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);
fMulti();

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the purpose of a console table?

546


What is a class c#?

550


What is sqlcommand in c#?

575


What is the difference between overriding and overloading in systemverilog?

537


What are the basic string operations? Explain.

593






What's the difference between abstraction and encapsulation?

553


how to prevent a class from being inherited in c#.net?

590


What is expression tree in c#?

569


What is the use of private constructor in c#?

569


Why singleton class is sealed?

583


What is inheritance c#?

583


Difference between type constructor and instance constructor? What is static constructor, when it will be fired? And what is its use?

626


What are the types of methods in c#?

552


What do you mean by the delegate in c#?

597


can you declare an override method to be static if the original method is not static?

632