What is a delegate, How many types of delegates are there

Showing Answers 1 - 6 of 6 Answers

VIVEK KUMAR

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

  Was this answer useful?  Yes

rayhan21

  • Dec 21st, 2006
 

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

  Was this answer useful?  Yes

arulfcs

  • Sep 7th, 2010
 

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.


  Was this answer useful?  Yes

deepak kumar choudhary

  • 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
  1. using System;

  2.  

  3. namespace Akadia.NoDelegate

  4. {

  5.     public class MyClass

  6.     {

  7.         public void Process()

  8.         {

  9.             Console.WriteLine("Process() begin");

  10.             Console.WriteLine("Process() end");

  11.         }

  12.     }

  13.  

  14.     public class Test

  15.     {

  16.         static void Main(string[] args)

  17.         {

  18.             MyClass myClass = new MyClass();

  19.             myClass.Process();

  20.         }

  21.     }

  22. }

  23.  

  24.  

  Was this answer useful?  Yes

deepak kumar choudhary

  • 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
  1. using System;

  2.  

  3. namespace Akadia.BasicDelegate

  4. {

  5.     // Declaration

  6.     public delegate void SimpleDelegate();

  7.  

  8.     class TestDelegate

  9.     {

  10.         public static void MyFunc()

  11.         {

  12.             Console.WriteLine("I was called by delegate ...");

  13.         }

  14.  

  15.         public static void Main()

  16.         {

  17.             // Instantiation

  18.             SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

  19.  

  20.             // Invocation

  21.             simpleDelegate();

  22.         }

  23.     }

  24. }

  25.  

  26.  

  Was this answer useful?  Yes

SAIRAM

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

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions