.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Interview Question >> C# >> Post New Question Subscribe to Interview Questions

What's a delegate?

Posted By :Akhil Raj     Posted Date :September 18, 2010    Points :10   Category :C# 
A delegate object encapsulates a reference to a method.

You can also find related Interview Question to What's a delegate?  below: 

the difference between a delegate and an event

  
the difference between a delegate and an event

Hi


Here is the difference between a delegate and an event.

Delegate:

public class VikramDel

{

public delegate void VikramExampleDelegate(int num1,string str1);

public VikramExampleDelegate VikramDeleageteCallback;

}

Event:

public class VikramEvent

{

public delegate void VikramExampleEvent(int num1,string str2);

public event VikramExampleEvent VikramEventCallback;

}

So syntax wise there is only one difference that we have to use the event keyword with the event.

So the question comes why do we have a keyword when the same work can be done without using it. But there is a reason for the existence of the keyword event. Lets take an example how would a client work with this class

VikramDel V = new VikramDel();

V.VikramDeleageteCallback +=new

VikramDel.VikramExampleDelegate (this.VikDelegate);

Here we are adding a new target to the invocation list of the delegate. The same code will work with the other class also without any problem

VikramEvent V = new VikramEvent();

V.VikramEventCallback + =new

VikramEvent.VikramExampleEvent(this.VikDelegate);

But consider a case where by instead of adding a new target to the invocation list of the delegate if I simply set a delegate to a new delegate (The difference is with the + sign being not there).

VikramDel V = new VikramDel();

V.VikramDeleageteCallback =new

VikramDel.VikramExampleDelegate (this.VikDelegate);

This code will work fine here but the same will not work with an event.

So what it means is that if we use the event keyword no client class can set it to null. This is very important. Multiple clients can use the same delegate. After multiple client have added a function to listen to the callback of the delegate. But now one of the client sets the delegate to null or uses the = sign to add a new call back. This means that the previous invocation list will not be used any more. Hence all the previous client will not get any of the callback even if they have registered for the call back.

Hence we can say that the even keyword adds a layer of protection on the instance of the delegate. The protection prevents any client to reset the delegate invocation list. They can only add or remove the target from the invocation list. (More...)

What are the different compiler generated methods when a .NET delegate is compiled?

  
Compilation of a .NET delegate results in a sealed class with three compiler generated methods whose parameters and return values are dependent on the delegate declaration. The methods are,
Invoke()
BeginInvoke() and
EndInvoke(). (More...)

What's a multicast delegate?

  
It's a delegate that points to and eventually fires off several methods. (More...)

When TimerCallback delegate is used?

  
Many application have the need to call a specific method during regular intervals. For such situations we can use the System.Threading.Timer type in conjunction with a related delegate named TimerCallback. (More...)

C# delegate keyword is derived form which namespace?

  
C# delegate keyword is derived form System.MulticastDelegate. (More...)

what is the main difference between delegate and an event in c#?

  
Delegates and Events Both are related.

Dalegete is a function pointer which can able to store the address of any function with same prototype.

Event is a function handler which can handles or run the functions in same prototype of its delegate.

For handling events delegate is used. (More...)

what is Delegate?

  
When a class contains 10 methods and need to access those methods then we normally access these objects through obj.methodname() one by one.
But with the help of the delegate we can access all 10 methods a single time with the help of the delegate.

i)Delegate is a function pointer.
ii) It allows the programmer to encapsulate a reference to a method inside a delegate object.
iii) Delegates are type-safe(type-safe function pointer which means that the parameter list and the return type are known. ) and secure.
iv) Delegates are created at run time
v)Delegates allow methods to be passed as parameters.
Vi)Delegates can be chained together; for example, multiple methods can be called on a single event.
Vii)Delegates can be used to define callback methods.
viii)"Delegates are ideally suited for use as events "

(More...)

What's a delegate?

  
A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers. (More...)

What's a multicast delegate?

  
It's a delegate that points to and eventually fires off several methods. (More...)

What is Multicast Delegate?

  
A delegate which holds the reference of more than one method. that is called multicast delegate. (More...)

What is MultiCast Delegate ?

  
A single or uni cast delegate can be instantiated by only a single method . when a Delegatee can make use of more than one method then it is said to be Multicast Delegate. (More...)

What's a delegate?

  
A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers. (More...)

What's a multicast delegate?

  
A delegate that has multiple handlers assigned to it. Each assigned handler (method) is called. (More...)

Difference between event and delegate?

  
event:
1) Data member of a type(class/structure)
2)Declared inside a type(class/structure)

delegates.

delegate:
1)Datatype(reference type) that holds references of methods with
some signatures.also called as function pointer.
2)It may or may not be declared inside a class.
(More...)

Quick Links For Interview Questions Categories:
ASP.Net Windows Application   .NET Framework   C#   VB.Net   ADO.Net  
Sql Server   SharePoint   Silverlight   OOPs   JQuery   JavaScript/VBScript
Biztalk Patten/Practices .IIS WCF WPF WWF
Networking Aptitude Others   All      

Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.

 
Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.

So, Start looking our Interview Question section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.


Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend