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

Define delay signing?

0 Answers  

What are the the three types of DAO ?

1 Answers  

What is Implicit conversion in C#?

0 Answers  

what are the differences b/w structure and class?

10 Answers  

What are the 3 different types of arrays?

0 Answers  

How do I enforce const correctness in c#?

0 Answers  

Practcle scenario where I can use abstract class and where I can use Interface

1 Answers   Jeevan Technologies,

What is file extension of webservices?

0 Answers  

List down the reason behind the usage of c# language.

0 Answers  

Define an abstract class?

0 Answers  

Can you change the value of a variable while debugging a C# application?

1 Answers  

How you will connect to windows directory in c#?

2 Answers   HP, nTech Solutions,