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.
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.
The only real advantages of delegates over interfaces are
When .net did not support generics, delegates were essential part of it since declaring distinct non-generic interfaces for each and every different function signature one would want to pass would have been unworkable. If .net had supported generics from the beginning, delegates would not have been necessary except for certain scenarios involving Reflection, and even there it would perhaps have been most useful to have the type
Action<T,U>
be an implementation ofIAction<T,U>
(so that code which simply needs something it canInvoke
would use the interface). An interface-based approach would have required the creation of single-method classes in cases where classes need to create delegates exposing multiple methods, but would have eliminated the need to create separate delegate instances in the many common cases where the number of methods of a given signature to be exposed is precisely one.Incidentally, replacing delegates with interfaces would in no way prevent the creation of a general-purpose Combine method. Indeed, interface covariance could make such a method work better than the existing
Delegate.Combine
in many respects. Implementing a method analogous toDelegate.Remove
would be at best clunky and annoying, but I can think of no situation other than event subscription management which would require the use ofDelegate.Remove
, and event subscription could best be handled using other approaches anyway.Yes, delegates are in many ways like single-method interfaces. However:
The last point is the most important one - consider a LINQ expression of:
Now imagine if to express the logic of
x > 5
andx * 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
andfor
. Do we really need them when we've gotgoto
? 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.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.
From When to Use Delegates Instead of Interfaces (MSDN):
From Delegates Vs Interfaces in C#: