Win Surprise Gifts!!!
Top 5 Contributors of the Month
Post New Question
Subscribe to Interview Questions
What does delegates do?
Posted By :
Posted Date :
September 09, 2008
Delegates enable scenarios that some other languages have addressed with function pointers. However, unlike function pointers, delegates are object-oriented and type-safe.
You can also find related Interview Question to
What does delegates do?
What is the differences between delegates and interfaces?
Interfaces carry semantics, and when a programmer implements an interface, he is typically well aware of that semantics. When you try to invoke a particular method via an interface, you can be fairly certain that if you succeed, the semantics of that method is what you expect. For that reason, using interfaces is essentially doing a check for semantic correctness on some level.
Delegates, on the other hand, by only verifying the method signature, make the programmer responsible for ensuring that the semantics of the method is compatible. The semantics may cover not only the meaning of the arguments and return value (some times even the order of the arguments if they are of the same type), the ranges of the arguments, but also an invocation order when multiple methods are concerned. Hence, in a sufficiently large program there is plenty of margin to make an error when different programmers are not forced to comply with a uniform semantics (as they would be if interfaces were used).
What Are the Generic Delegates?
The System namespace defines five new generic delegates. The first is EventHandler
public delegate void EventHandler
(object sender,E e) where E : EventArgs
Why .NET delegates are called type safe?
.NET delegates are type safe. If you will attempt to pass a delegate, a method that does not "match the pattern", you will receive a compile time error "Method does not match delegate pattern!".
What is the use of GetInvocationList() in C# delegates?
GetInvocationList() returns an array of System.Delegate types, each representing a particular method that may be invoked.
Which one is similar to Delegates?
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
What are delegates?where are they used ?
A delegate defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.
When would you use delegates in C#?
In C# where we always had to have a separate method to implement the logic, using a delegate often didn't make sense. in that case we can use delegates for:
a) Event handlers (for GUI and more)
b) Starting threads
c) Callbacks (e.g. for async APIs)
d) LINQ and similar (List.Find etc)
e) Anywhere else where I want to effectively apply "template" code with some specialized logic inside (where the delegate provides the specialization)
In simple word
a) For event handler
b) To pass method in a method parameters
what is delegates?
It is an object that can refer to a method.
what is the use of Delegates?
To Pass methods of One Class to objects of other cloasses that can call those methods.
you Can pass both static and instance Methods.
C# delegates are implemented in the .Net Framework as a Class derived from System.Delegate.
Use of Delegate:
Declare Delegate Object with a Signature that exactly matches the method Signature that you are trying to encapsulate.
Define all methods whose signature match the signature of the Delegate object that you have defined in step1.
Create delegate object and plug in the methods that you want to encapsulate.
Call the Encapsulate method through the Delegate Object.
what are different types of Delegates?
1. Single cast delegate 2. Multicast delegate.
What is Delegates
A Delegates in C# are a type-safe, object-oriented implementation of function pointers and are used in many situations where a component needs to call back to the component that is using it.
Quick Links For Interview Questions Categories:
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
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
Terms of Service
Tell A Friend