tags:

views:

367

answers:

5

I've read two books, tons of examples. They still make next to no sense to me. I could probably write some code that uses delegates, but I have no idea why. Am I the only one with this problem, or am I just an idiot? If anyone can actually explain to me when, where, and why I would actually use a delegate, I'll love you forever.

+8  A: 

Delegates are just a way to pass around a function in a variable.

You pass a delegated function to do a callback. Such as when doing asynchronous IO, you pass a delegated function (a function you have written with the delegate parameter) that will be called when the data has been read off the disk.

Byron Whitlock
That actually makes more sense then I have seen so far, thanks.
Kin
In my opinion, some of the initial learning curve stems from the delegate syntax. When you first start off, it feels like a whole lot of ceremony. To make matters worse, it can feel like everyone else but you thinks it's all so simple. (Not criticizing the syntax... just recalling my learning experience.)
Larsenal
It does feel like that. I've seen examples that tries to explain them, has examples, and proceeds to say "Now isn't that great?", while I was just scratching my head.
Kin
This is how I think of a delegate. It's a variable containing a method. You can have a collection of them, you can cast them, you can define them... etc
Chuck Conway
+2  A: 

As other people have mentioned delegates are handy for callbacks. They're useful for a whole load of other things too. For example in a game I've been working on recently bullets do different things when they hit (some do damage, some actually increase the health of the person they hit, some do no damage but poison the target and so on). The classical OOP way to do this would be a base bullet class and a load of subclasses

Bullet
    DamageBullet
    HealBullet
    PoisonBullet
    DoSomethingElseBullet
    PoisonAndThenHealBullet
    FooAndBarBullet
    ....

With this pattern, I have to define a new subclass every time I want some new behavior in a bullet, which is a mess and leads to a lot of duplicated code. Instead I solved it with delegates. A bullet has an OnHit delegate, which is called when the bullet hits an object, and of course I can make that delegate anything I like. So now I can create bullets like this

new Bullet(DamageDelegate)

Which obviously is a much nicer way of doing things.

In functional languages, you tend to see a lot more of this kind of thing.

Martin
So what you're saying is that there's no SilverBullet?
quillbreaker
Nonsense, they're just expensive and will only tend to be used JIT ;)
drachenstern
+3  A: 

Maybe you want to read this very interesting blog article about Higher-Order Functions implemented in C#. It is not the easiest stuff - but kind of real world examples with heavy usage of delegates. Maybe it helps you to understand what their purpose is.

tanascius
The first code example really cleared a lot up for me, thank you.
Kin
+3  A: 

A delegate is a simple container that knows where in the machine's memory a specific method is located.

All delegates have an Invoke(...) method, thus when someone has a delegate, he can actually execute it, without really having to know or bother what that method actually does.

This is especially helpful for decoupling stuff. GUI frameworks wouldn't be possible without that concept, because a Button simply can't know anything about your program you're going to use it in, so it can't call your methods by itself whenever it is clicked. Instead, you must tell it which methods it should call when it is clicked.

I guess you're familiar with events and you do use them regularly. An event field is actually a list of such delegates (also called a multi-cast delegate). Maybe things will become clearer when we look at how we could "simulate" events in C# if it didn't have the event keyword, but only (non-multicast) delegates:

public class Button : Rectangle
{
    private List<Delegate> _delegatesToNotifyForClick = new List<Delegate>();

    public void PleaseNotifyMeWhenClicked(Delegate d)
    {
        this._delegatesToNotifyForClick.Add(d);
    }

    // ...

    protected void GuiEngineToldMeSomeoneClickedMouseButtonInsideOfMyRectangle()
    {
        foreach (Delegate d in this._delegatesToNotifyForClick)
        {
            d.Invoke(this, this._someArgument);
        }
    }
}

// Then use that button in your form

public class MyForm : Form
{
    public MyForm()
    {
        Button myButton = new Button();
        myButton.PleaseNotifyMeWhenClicked(new Delegate(this.ShowMessage));
    }

    private void ShowMessage()
    {
        MessageBox.Show("I know that the button was clicked! :))))");
    }
 }

Hope I could help a little. ;-)

herzmeister der welten
+2  A: 

This article from Chris Sells might help:

.NET Delegates: A C# Bedtime Story

Jay Riggs
+1. I think of delegates as single method interfaces. "So, he decided to break the methods out of the interface into separate delegate functions, each of which acted like a little tiny interface of one method each"
kenny
Another great example, thank you.
Kin