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

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);
d();
}
}

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
callbacks.
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
System.Object,
ByVal e As System.EventArgs) Handles cmdDelegates.Click
Dim objDelegateAddString As DelegateAddString
objDelegateAddString = AddressOf AddString
objDelegateAddString.Invoke()
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
pointer
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);
{
returnx+y;
}
}
class sample
{
svm()
{
sample prog.Mathm=new sampleprog.math();
deld=new del(m.add);
c.w(d(48.97,27.29));
d=new del(m.sub);
c.w(d(29.29,7.92));
c.r
}
}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Sharp Interview Questions

hi, I am a begineer to c sharp. I have written a code for finding out prime numbers. Can anyone identify what are the flaws in my code. Kindly donot complex the code or present logic because i am new to c sharp and just started learning programming language.Thanks in advance. class Program { static void Main(string[] args) { int a,b=1; a = int.Parse(Console.ReadLine()); c= int.Parse(Console.ReadLine()); if (a % b == 0 && a % 2 != 0 && a % a == 0) Console.WriteLine(a); else if (a % b == 0 && a % 2 == 0) Console.WriteLine(a%2); Console.WriteLine("Number is not PRIME"); Console.ReadLine(); } } }

2 Answers  


Can abstract class be instantiated c#?

0 Answers  


how to create crystal reports give one detail example(i want to view age category report) please give suitable example in my small knowledge

0 Answers  


what r arraylist? what the use of Hashtables?

4 Answers  


what are some characteristics of an array?

0 Answers  






How can you prevent escaping on backslashes in C# with string definitions?

0 Answers   Siebel,


Explain About friend and Protected friend

0 Answers  


What are escape sequences explain with example?

0 Answers  


What is difference between private, protected, and public in C#?

0 Answers   B-Ways TecnoSoft,


What is the difference between static and private constructor?

0 Answers  


Why c# is called c sharp?

0 Answers  


What are the different states of a thread?

0 Answers  


Categories