Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegates vs Interfaces in C#

I would like to pose this question as long as I am trying currently to dig into the use and the purpose of delegates, although it is likely to have been asked in similar formulations.

I know that delegates serve as function pointers used in C++. The matter of the fact is if in C# they serve mostly as an alternative to interfaces and polymorphism. Since I can create subclasses of a specific class and supply them the appropriate methods to each one, what offer delegates additionally to that? Are there cases that stipulate their use or is merely the maintainability of the code improved when delegates used? Would you recommend their wide deployment over interfaces?

I am speaking solely about delegates and I want to distinguish their role from the events role.

like image 837
arjacsoh Avatar asked Jan 01 '12 19:01

arjacsoh


People also ask

What is difference between interface and delegate?

Delegates and Interfaces are two distinct concepts in C#. Interfaces allow to extend some object's functionality, it's a contract between the interface and the object that implements it, while delegates are just safe callbacks, they are a sort of function pointers.

Which is faster to execute delegate or interface?

Delegates are faster because they are only a pointer to a method. Interfaces need to use a v-table to then find a delegate; They are equal, but delegates are easier to use.

Can I declare delegate in interface?

It's the base of design in the Object Oriented Programming. These are basically declaring delegate types, so they don't belong in an Interface. You can use Event in Interface with Delegate type.


2 Answers

Yes, delegates are in many ways like single-method interfaces. However:

  • There is support built into the CLR for them
  • There's support in the framework for them, including multi-cast abilities and asynchronous invocation
  • There's additional C#/VB language support in the form of method group conversions, lambda expressions, anonymous methods
  • They're mandated for events (i.e. events and delegates are a sort of matching pair)
  • They mean you don't need to implement an interface in a separate class for each delegate instance you want to create.

The last point is the most important one - consider a LINQ expression of:

var query = collection.Where(x => x > 5)                       .Select(x => x * x); 

Now imagine if to express the logic of x > 5 and x * x you had to write a separate class for each expression, and implement an interface: the amount of cruft vs useful code would be ridiculous. Now of course the language could have been designed to allow conversions from lambda expressions into interface implementations via separate classes, but then you'd still lose the benefit of being able to simply write a separate method and create a delegate with that as the target. You'd also still lose the multi-cast abilities.

As a similar thought exercsise, consider looping statements such as while and for. Do we really need them when we've got goto? Nope. But life is much better with them. The same is true of delegates - and indeed properties, events, etc. They all make the development simpler.

like image 125
Jon Skeet Avatar answered Sep 20 '22 07:09

Jon Skeet


The biggest practical difference is that you can provide different delegate instances for the same delegate from the same class, while you cannot do it with interfaces.

delegate void XYZ(int p);  interface IXyz {     void doit(int p); }  class One {     // All four methods below can be used to implement the XYZ delegate     void XYZ1(int p) {...}     void XYZ2(int p) {...}     void XYZ3(int p) {...}     void XYZ4(int p) {...} }  class Two : IXyz {     public void doit(int p) {         // Only this method could be used to call an implementation through an interface     } } 
like image 32
Sergey Kalinichenko Avatar answered Sep 21 '22 07:09

Sergey Kalinichenko