VIVEK KUMAR
Answered On : Sep 30th, 2006
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 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.
public delegate int Comparer(object obj1, object obj2);
This delegate declaration defines the signature of a delegate handler method that this delegate can refer to. The delegate handler method, for the Comparer delegate, can have any name, but must have a first parameter of type object, a second parameter of type object, and return an int type.
Login to rate this answer.
A delegate is a strongly type of pointers.through delegate we can call any function even the class is sealed & with delegate we can create our own events to the controls
there are 2 type of delegate
they are 1.single delegate 2.multicast delegate
Login to rate this answer.
Delegate is a object that reference a method and it is type safe. It is like
C & C++ pointer if you assign delegate a method it will behave like a method
that can be used like method with parameters and returns a value.
It has 2 types.
1. Single cast delegates-it refer a one method at a time
2. Multicast delegates-it refer more than one method.
Login to rate this answer.
deepak kumar choudhary
Answered On : Oct 12th, 2011
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
Code
using System;
namespace Akadia.NoDelegate
{
public class MyClass
{
public void Process()
{
Console.WriteLine("Process() begin");
Console.WriteLine("Process() end");
}
}
public class Test
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.Process();
}
}
}
Login to rate this answer.
deepak kumar choudhary
Answered On : Oct 12th, 2011
A delegate will allow us to specify what the function we'll be calling looks like without having to specify which function to call. The declaration for a delegate looks just like the declaration for a function, except that in this case, we're declaring the signature of functions that this delegate can reference.
Code
using System;
namespace Akadia.BasicDelegate
{
// Declaration
public delegate void SimpleDelegate();
class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}
public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
// Invocation
simpleDelegate();
}
}
}
Login to rate this answer.
SAIRAM
Answered On : Oct 23rd, 2011
A delegate is a reference type variable , which holds the reference of a method. This reference can be changed at run time , as desired.
Two types of delegates :-
1. single-cast delegate - can call only one method at a time (derived from system.delegate)
2. multi-cast delegate - can call multiple methods at the same time. (derived from system.multicast.delegate)
Login to rate this answer.