Los Techies : Blogs about software and anything tech!

What's The Point Of Delegates In .NET?


A coworker just asked me this question - what's the point of delegates in .NET? My answer was very short and one that he had not found online: to delay execution of a method.

Consider this: if you pass a Method2 as a parameter to Method1, Method2 is evaluated immediately and the result is passed into Method1.

public class SomeObject
{
    public void Method1(int someValue)
    {
        //... code here
    }
}
 
...
 
someObject.Method1(anotherObject.Method2());

Additionally, Method2 must have a return value (not void) so that the value returned from Method2 can be passed as the parameter of Method1.

With a delegate, though, we get method pointers that can be used to delay the execution of the method in question until it's needed (if at all). We can also get rid of the requirement for a return value and we can pass values that were created by the host method, into the delegate method.

public class SomeObject
{
    public void Method1(Action<int> someAction)
    {
        //.. do some stuff here
        int aValue = GetTheValueFromSomeWhere();
        someAction(aValue);
        //... do some more here
    }
}
 
public class AnotherObject
{
    public void Method2(int aValue)
    {
        //do something that is dependent on aValue, here.
        int i = aValue + 1; // or anything else that needs aValue.
    }
}

FYI - Action (void return; with many parameterized variations) is a delegate that is build into .NET 3.5, along with Func<T> (return value of type T; with many parameterized variations). These are the two most common delegates that I use in my code.

Most .NET developers have used delegates without realizing it, too. The event system in .NET is nothing more than a multi-cast delegate - a delegate that points to more than one method - with a special signature and syntax (depending on the language being used).

There's a lot of good use for delegates. And like any other tools, there's a lot of bad uses. Take the time to learn what they are, how they work, and where they can be beneficial.

Kick It on DotNetKicks.com
Posted Oct 09 2008, 03:05 PM by derick.bailey

Comments

Frank Quednau wrote re: What's The Point Of Delegates In .NET?
on 10-10-2008 2:22 AM

Indeed, that characteristic of delegates came useful to an idea we are developing, which is basically a set of classes providing configuration APIs for setting up hierarchies of Windows Forms Controls. You don't really want to create the Control hierarchy when you are using the config API, instead delegates are set up in the proper way, and once the user calls the Setup(Control c) method all Action<Control> delegates created during the configuration can be executed. This also provides the advantage that youc an reuse such a Control Setup instance.

Reflective Perspective - Chris Alcock » The Morning Brew #198 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #198
on 10-10-2008 2:24 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #198

links for 2008-10-10 wrote links for 2008-10-10
on 10-10-2008 8:31 AM

Pingback from  links for 2008-10-10

Maxim wrote re: What's The Point Of Delegates In .NET?
on 10-10-2008 8:37 AM

Never thought about the "delaying execution". Hummm thanks :)

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew
on 10-10-2008 8:48 AM

Pingback from  Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew

Arjan`s World » LINKBLOG for October 10, 2008 wrote Arjan`s World &raquo; LINKBLOG for October 10, 2008
on 10-10-2008 2:53 PM

Pingback from  Arjan`s World    &raquo; LINKBLOG for October 10, 2008

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew
on 10-12-2008 5:05 PM

Pingback from  Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew

Weekly Link Post 63 « Rhonda Tipton’s WebLog wrote Weekly Link Post 63 &laquo; Rhonda Tipton&#8217;s WebLog
on 10-12-2008 7:14 PM

Pingback from  Weekly Link Post 63 &laquo; Rhonda Tipton&#8217;s WebLog

OJ wrote re: What's The Point Of Delegates In .NET?
on 10-13-2008 10:31 PM

I think that your summation is PART of the answer, but not an answer as a whole. Delegates are used in scenarios where you are not delaying the execution of code, such as varying functionality based on state.

derick.bailey wrote re: What's The Point Of Delegates In .NET?
on 10-14-2008 4:03 PM

@OJ

The very nature of delegates will always be a delayed execution of the code they point to, no matter the intended use of the delegate.

public void Foo(Action bar)

{

  bar();

}

or

Action bar = new Action(someMethod);

bar();

these are the most basic examples of using a delegate, and both of them are illustrations of delayed code execution - whether it's one wait cycle that is imperceptable by a human is irrelevant. The runtime is delaying the execution of the delegate's target until the delegate is invoked.

new ThoughtStream("Derick Bailey"); wrote PTOM: Command and Conquer Your UI Coupling Problems
on 11-19-2008 9:23 PM

This post is part of the November 2008 Pablo's Topic Of The Month (PTOM) - Design Patterns and will

Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems