Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

How do you go about teaching Delegates to new programmers? Any examples outside the programming concept that you would use to teach them?

For instance, with inheritance, you teach it as follows.

Cat is an Animal. Dog is an Animal too.

So, we can put the common features that all Animals have in the Animal group (read as class) and then put the specific features that Cats have in the Cat group (read as class).

In a similar way (or a different way), how would you teach Delegates?

share|improve this question
    
You mean typed function pointers? –  C. Ross Dec 15 '10 at 20:50
add comment

9 Answers

up vote 9 down vote accepted

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. In reality:

Delegate object is an envelope. In the envelope (delegate object) there is a phone number (referenced method) to an unknown person (method), the receiver of envelope (delegate object) can call that number (referenced method), now here you can tell (delegate parameter) that person to do something interesting.

share|improve this answer
add comment

Just tell it how it is.

Imagine I am your boss (a fearsome prospect, I know). One day, I show up with a colleague and say to you:

"I delegate responsibility for doing something to this person. They are my delegate, and you should ask them if you need anything done. They may ask me to do it, or they may do it themselves. Either way, it gets done and you don't need to know who did it."

That's it.

All your programmers have to do now is understand that there is a hidden reference to an object behind the presented delegate.

share|improve this answer
add comment

Well, if they have a background in C/C++, the easiest way to explain it is probably to relate them to function pointers. That's how I originally thought about them when I transitioned to .NET.

share|improve this answer
    
So it is just a call-back? –  Bjarke Freund-Hansen Dec 15 '10 at 15:18
    
@bjarkef, yes that is another way to think about it. –  RationalGeek Dec 16 '10 at 13:54
add comment

It's like finding 4 cans of beer on the shelf in store. But instead of opening and drinking all of them right there you take them home. Maybe your friend will have one? Why not let him open it?

share|improve this answer
    
What? That's not what delegates are at all... –  Bartek Tatkowski Dec 15 '10 at 12:57
    
@Bartek - actually, apart from being funny and concise, I find radekg's explanation to be logically correct too. So, +1. –  Jas Dec 15 '10 at 14:26
add comment

a delegate is a reference to a specific object's method; it's like an I.O.U. for a function call.

share|improve this answer
    
Agree with the IOU with the distinction that it may be redeemed multiple times :) –  Otávio Décio Dec 15 '10 at 20:34
add comment

A delegate is like a blank cheque. You can write a cheque for bank accounts you have access to. You can give that cheque to someone else, who perhaps does not have access to that bank account. The cheque can pass hands until someone writes in an amount and cashes it in, which instructs the bank to withdraw money from that account and give it to that person. The person caching in the cheque might not even know the person who wrote the cheque or the owner of the bank account.

Cheque = delegate
Bank account = delegated object
Withdrawal = delegated method
Transfer amount = arguments passed to the delegate
Withdrawn money = return value from the delegate
Writing the cheque = creating the delegate
Passing the cheque = passing the delegate
Caching in the cheque = activating the delegate

Unlike cheques, with delegates you can:

  • Activate it multiple times.
  • Create it so that it instructs anyone to perform any action, and not only instruct a bank account to withdraw money.
  • Depending on the delegate type, you might not be able to specify arguments, or might not get a return value
share|improve this answer
add comment

Our customers tell my boss "make the program run faster".

My boss might tell me or any of my colleagues "make the program run faster".

"make the program run faster" gets done for the customer, but the customer does not know who's doing it. They only talk to my boss.

share|improve this answer
1  
foo is bad example. refactor foo (its a bad smell) –  Display Name Dec 15 '10 at 12:14
add comment

The best example I ever heard is a parent-teenager relationship. A teenager's friend asks her to hang out on a Friday night, but the teenager has to ask her parents (her delegate) for permission first.

share|improve this answer
add comment

If they have a background (C/C++) for it then explaining that it's a strongly typed function pointer may be sufficient albeit not 100% accurate. I honestly felt that I really grokked them once I started working with LINQ to objects.

var results = myListOfInt.Where(x=>x> 5);

is equivalent to

Func<int, bool> myFunc = x=> x > 5;
var results = myListOfInt.Where(myFunc);

is equivalent to

public class ....
{
    private bool SomeFunc(int x){ return x > 5}

    public void DoSomething()
    {
        var results = myListOfInt.Where(SomeFunc);
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.