#372 – What Delegates Are Good For

Creating an instance of a delegate that allows you to invoke a method through the delegate doesn’t seem to add much value.  Why not just invoke the method directly?

            // New delegate instance
            LoggerDelegate del1 = Method1;

            // Can still invoke the method directly
            Method1("Direct");

            // Or can invoke through the delegate instance
            del1("Via delegate");

A delegate allows us to treat the reference to the method as a real object, storing it in a variable or passing it to a method as a parameter.  This allows great flexibility, because a method can be told at runtime which child method to call.

        static void DoSomeCalculations(LoggerDelegate log)
        {
            // Do some stuff

            log("I did stuff");

            // Do other stuff

            log("And I did some other stuff");
        }

You then pass in the specific logging method when you invoke DoSomeCalculations and it will call that method.

            DoSomeCalculations(Method1);
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

9 Responses to #372 – What Delegates Are Good For

  1. kai zhou says:

    Nice work, thank you Sean.

  2. Kristof says:

    Shouldn’t you pass del1 instead of Method1 as the argument? As far as I understood from your post that’s the whole point of delegate!

  3. Sean says:

    Kristof, note that you can pass either a delegate instance or a method name to a function that has a delegate type as one of its parameters. (E.g. DoSomeCalculations()). Similar to if you had a parameter of type int and you could either pass a integer typed variable or just an integer constant. You can think of the method name as like a delegate type constant.

  4. Mike Lee says:

    Sean, thanks for this. It wasn’t obvious to me at first, but I think I understand the significance now of using the delegate to use pass a method as an argument. To follow up, would there need to be an instance of a delegate declared for each method before it is passed? (which seems, odd and unintuitive) Assuming you don’t have to, could you then pass any method through the delegate parameter?

    • Sean says:

      Thanks Mike. No, you don’t need to define a delegate instance at all, but just pass the method name to the method that takes a delegate type as a parameter. A delegate instance will get created for you.

  5. Yume says:

    Thank you, I’ve never understood the use of delegates before! Been on your blog all day.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: