What is a delegate?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
What is InterFace???
What are tuples c#?
Why are strings immutable in c#?
How to achieve polymorphism in c#?
what are wrapper classes?
What is the difference between iqueryable and ienumerable?
Can you inherit multiple classes in c#?
How to reduce image resolution in C#?
How do I start a program in c#?
Can you prevent your class from being inherited and becoming a base class for some other classes?
Explain about ODP.net
How is method overriding different from overloading?