What is a delegate?

Answers were Sorted based on User's Feedback

What is a delegate?..

Answer / 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

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

Is This Answer Correct ?    4 Yes 0 No

What is a delegate?..

Answer / sadab alam

Delegate is refrence type varible that holds the refrence
to a method.
There are two type of delegte
1.Sigle cast delegate
2.Multi cast delegate
if delgate variable contains refrence of one method then it
is called sinlecast delegate
and if contains refrence of more than one method then it is
called multicast refrence.

using System;
public delegate void mydel();
class del
public static void method1()
Console.WriteLine("first method");
public static void method2()
Console.WriteLine("Second method");

public static void Main()
mydel d=new mydel(method1);
d+=new mydel(method2);

Is This Answer Correct ?    4 Yes 0 No

What is a delegate?..

Answer / kiran

Delegate is a class that can hold a reference to a method
or a function.Delegate class has
a signature and it can only reference those methods whose
signature is compliant with the
class.Delegates are type-safe functions pointers or
Below is a sample code which shows a example of how to
implement delegates.
Public Class FrmDelegates
Inherits System.Windows.Forms.Form
Public Delegate Sub DelegateAddString()
Private Sub FrmDelegates_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub AddString()
lstDelegates.Items.Add(“Running AddString() method”)
End Sub
Private Sub cmdDelegates_Click(ByVal sender As
ByVal e As System.EventArgs) Handles cmdDelegates.Click
Dim objDelegateAddString As DelegateAddString
objDelegateAddString = AddressOf AddString
End Sub
End Class
In the above there is a method called “AddString()” which
adds a string to a listbox.You
can also see a delegate declared as :-
Public Delegate Sub DelegateAddString()
This delegate signature is compatible with the “AddString”
method.When i mean
compatibility that means that there return types and
passing parameter types are same.Later in command click of
the button object of the Delegate is created and the method
is received from “AddressOf ” keyword.Then by using
the “Invoke” method the method
is invoked.

Is This Answer Correct ?    1 Yes 0 No

What is a delegate?..

Answer / mahesh babu ummaneni

Deligate is nothing but method pointer.using this we can call nomber of time of single method or multiple methods
we dont need call metod directly
we want call deligate it will call methods

Is This Answer Correct ?    0 Yes 0 No

What is a delegate?..

Answer / venkat2050

delegate is a wrapper we can call any method at runtime to
call delegate by calling the keyword del
using system;
namespace sample prog
public delegate double(doublex,doubley);
class math
public double add(doublex,doubley);
class sample
sample prog.Mathm=new sampleprog.math();
deld=new del(m.add);
d=new del(m.sub);

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Sharp Interview Questions

Will finally block get executed if the exception had not occurred?

2 Answers  

What is short circuit logical evaluation?

1 Answers   TCS,

What is delay signing..??

1 Answers   HCL,

In .NET how can you solve the DLL Hell problem?

0 Answers   Siebel,

What are the collection classes ?

2 Answers   Digital GlobalSoft,

write code for inserting and updating recoards in sql server database using connected architecture & records shoulds displayed in grid view ( use c# )

0 Answers  

What is the difference b/w Arry.copy() and array.clone() method?

6 Answers  

What is a trace and asset? Explain some differences?

0 Answers   Accenture,

What is overloading and how can this be done ?

4 Answers   MMTS,

how to Create a datagridview control with check box column with 8rows in it, the maximum number of check boxes checked should be 3, when user checks the 4th corresponding message should be displayed and check box should be checked. User can uncheck the checked boxes Note: read-only property should not be used

0 Answers   HCL, NIC,

User's session is explicitly killed by which method ?

0 Answers   Siebel,

what is a delegate? what it is used for?

13 Answers   TCS, Choice Solutions,