What is a delegate?

_config.yml

Delegates:

In my zeal to spare others the pain of wading through the bog of frustration, I thought to provide a simpler explanation of what a delegate is. There are a couple of examples.

What is a delegate?

You can think of delegates as basically a list of chores. At first the list is empty. But you can add stuff to it. Let’s add vacuuming to an otherwise empty list.

Definition: A delegate is basically a to-do list. A list of chores.

Ok so we have our chores list with vacuuming on it:

List of chores:

  1. Vacuum

Then you realise it’s been 5 days since you last washed your underpants so you add that to the list too:

List of chores:

  1. Vacuum
  2. Do washing.

And that’s basically it. A delegate is simply a list of chores. And when you do actually perform what’s on this list of chores (tomorrow etc) you will be “invoking” the chores, or, “invoking the delegate”

Definition: The doing of the chores is called: “invoking the delegate”.

What’s so good about delegates?

Have you seen that movie about mind control: The Manchurian Candidate? I haven’t. But it basically goes like this. Someone is being mind-controlled in a conspiracy to assassinate a political leader. Think about normal assassination attempts: the assassin has to have knowledge about his planned assassination; he has to carefully plan the logistics of how he’s gonna do it, and his escape. The problem is that is if he is caught, he’ll be interrogated, and because the assassin had a good idea that he is in fact an assassin, and that he is going to attempt the murder by shooting the President in a theatre, it’s pretty easy for the police to work out whether he’s guilty or not.

Now let’s consider the possibility of an assassin who doesn’t even know that he’s an assassin. Think about it: he’s the perfect criminal. If caught, he can honestly plead innocence and he’ll be very, very believable. But because this particular assassin is being mind-controlled, he’ll be able to commit the crime without having any knowledge about: (i) what to do, and (ii) how to do it. All he does know is that he must follow some orders for someone in the future. The specifics of that particular order? He doesn’t know. At some future point in time, somebody will whisper in his ear: “activate”, and then our mind controlled zombie will run off and commit his assassination without question and with no knowledge of techniques etc to do it. But he’ll still do it faithfully. Amazing! That’s basically what a delegate is.

Summary

You hand over someone a list of chores in a secret envelope. And at some future point in time, this person opens the envelope and performs the chores.

But wait…..there are more details

But it does get a wee little bit more complicated. There are different types of chores lists available. There’s a washing list, and a cleaning list, or a shopping list etc.

Rule 1: You can only add certain types of chores to certain types of lists. What does this mean?

  • It means that you can only add chores pertaining to washing in the washings list. (e.g. washing a car, washing clothes etc). If you try to add a “buy milk” task to the list then: ERRRRR – error: it just won’t work.

  • Delegates require you to specify up front what type of chores can be added to the list. E.g. I can have a delegate (or a list) which only allows people to add (or remove) chores pertaining to washing, (or ironing, or dusting as the case may be). I can set the rules as to what can be added or taken away.

Finally, when you’ve collected your list, you can simply hand it over to someone and say: “execute” and then all those chores will be completed. That’s basically the concept of delegates.

Summary:

  1. You create the list.

  2. The person performing the chores knows nothing of the specifics of the chores.

  3. You simply hand over the list and he or she performs the chores.

  4. The person doing the chores knows that someone might hand him/her a list of chores.

  5. The person doing the chores only knows how to do certain types of chores. So you can’t ask him/her to do something they can’t actually do.

Let’s see how it works in code:

Let’s declare a delegate:

public delegate void Speak(string a);
            // declares a delegate. Any "chores" added to this list must:
            // (i) return a void, and (ii) must accept only 1 parameter and
            // that parameter must be a string.

Now let’s instantiate a delegate:

            // this is the chore we want to add to the chores "list"

            private void Yell(string input)
            {
                Console.WriteLine(input.ToUpper());
            }

Now let’s actually add it to the list:

                // basically we are adding the yell method to the chores list.
                // we are calling this list: "test"

                Speak test = new Speak(Yell);

Now let’s actually do the chores:

test("Let's go!");
              // prints to console ==> LET'S GO!

It’s that simple.

  Even more Specifics:

• You can add more methods to the list.

• You can remove methods from the list.

• You can add (i) anonymous methods and (ii) lambda expressions to delegates. These two forms (i.e. anonymous methods and lambda expressions) are generally referred to as anonymous functions.

Example of an anonymous method being added to a delegate:

               Speak test1 = delegate(string a) { };

Example of a lambda expression being used to instance a delegate:

Speak test2 = (x) => { };

That’s basically what a delegate actually is. There’s a lot here to grasp. So I will post another explanation tomorrow and will come at it from a different point of view.

Simple, wasn’t it? Hopefully it was – and that would mean I’ve done my job.

Written on February 8, 2017