Why do we need C# delegates

C#Delegates

C# Problem Overview


I never seem to understand why we need delegates? I know they are immutable reference types that hold reference of a method but why can't we just call the method directly, instead of calling it via a delegate?

Thanks

C# Solutions


Solution 1 - C#

Simple answer: the code needing to perform the action doesn't know the method to call when it's written. You can only call the method directly if you know at compile-time which method to call, right? So if you want to abstract out the idea of "perform action X at the appropriate time" you need some representation of the action, so that the method calling the action doesn't need to know the exact implementation ahead of time.

For example:

  • Enumerable.Select in LINQ can't know the projection you want to use unless you tell it
  • The author of Button didn't know what you want the action to be when the user clicks on it
  • If a new Thread only ever did one thing, it would be pretty boring...

It may help you to think of delegates as being like single-method interfaces, but with a lot of language syntax to make them easy to use, and funky support for asynchronous execution and multicasting.

Solution 2 - C#

Of course you can call method directly on the object but consider following scenarios:

  1. You want to call series of method by using single delegate without writing lot of method calls.
  2. You want to implement event based system elegantly.
  3. You want to call two methods same in signature but reside in different classes.
  4. You want to pass method as a parameter.
  5. You don't want to write lot of polymorphic code like in LINQ , you can provide lot of implementation to the Select method.

Solution 3 - C#

Because you may not have the method written yet, or you have designed your class in such a way that a user of it can decide what method (that user wrote) the user wants your class to execute.

They also make certain designs cleaner (for example, instead of a switch statement where you call different methods, you call the delegate passed in) and easier to understand and allow for extending your code without changing it (think OCP).

Delegates are also the basis of the eventing system - writing and registering event handlers without delegates would be much harder than it is with them.

See the different Action and Func delegates in Linq - it would hardly be as useful without them.

Having said that, no one forces you to use delegates.

Solution 4 - C#

  1. Delegates supports Events
  2. Delegates give your program a way to execute methods without having to know precisely what those methods are at compile time

Solution 5 - C#

Anything that can be done with delegates can be done without them, but delegates provide a much cleaner way of doing them. If one didn't have delegates, one would have to define an interface or abstract base class for every possible function signature containing a function Invoke(appropriate parameters), and define a class for each function which was to be callable by pseudo-delegates. That class would inherit the appropriate interface for the function's signature, would contain a reference to the class containing the function it was supposed to represent, and a method implementing Invoke(appropriate parameters) which would call the appropriate function in the class to which it holds a reference. If class Foo has two methods Foo1 and Foo2, both taking a single parameter, both of which can be called by pseudo-delegates, there would be two extra classes created, one for each method.

Without compiler support for this technique, the source code would have to be pretty heinous. If the compiler could auto-generate the proper nested classes, though, things could be pretty clean. Dispatch speed for pseudo-delegates would probably generally be slower than with conventional delegates, but if pseudo-delegates were an interface rather than an abstract base class, a class which only needs to make a pseudo-delegate for one method of a given signature could implement the appropriate pseudo-delegate interface itself; the class instance could then be passed to any code expecting a pseudo-delegate of that signature, avoiding any need to create an extra object. Further, while the number of classes one would need when using pseudo-delegates would be greater than when using "real" delegates, each pseudo-delegate would only need to hold a single object instance.

Solution 6 - C#

Think of C/C++ function pointers, and how you treat javascript event-handling functions as "data" and pass them around. In Delphi language also there is procedural type. Behind the scenes, C# delegate and lambda expressions, and all those things are essentially the same idea: code as data. And this constitutes the very basis for functional programming.

Solution 7 - C#

You asked for an example of why you would pass a function as a parameter, I have a perfect one and thought it might help you understand, it is pretty academic but shows a use. Say you have a ListResults() method and getFromCache() method. Rather then have lots of checks if the cache is null etc. you can just pass any method to getCache and then only invoke it if the cache is empty inside the getFromCache method:

_cacher.GetFromCache(delegate { return ListResults(); }, "ListResults");

public IEnumerable<T> GetFromCache(MethodForCache item, string key, int minutesToCache = 5)
    {
        var cache = _cacheProvider.GetCachedItem(key);
        //you could even have a UseCache bool here for central control
        if (cache == null)
        {
            //you could put timings, logging etc. here instead of all over your code
            cache = item.Invoke();
            _cacheProvider.AddCachedItem(cache, key, minutesToCache);
        }            
        
        return cache;
    }

Solution 8 - C#

You can think of them as a construct similar with pointers to functions in C/C++. But they are more than that in C#. Details.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionInfoLearnerView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#TalentTunerView Answer on Stackoverflow
Solution 3 - C#OdedView Answer on Stackoverflow
Solution 4 - C#NandaView Answer on Stackoverflow
Solution 5 - C#supercatView Answer on Stackoverflow
Solution 6 - C#xiachengbinView Answer on Stackoverflow
Solution 7 - C#GarethReidView Answer on Stackoverflow
Solution 8 - C#Liviu MandrasView Answer on Stackoverflow