views:

362

answers:

3

I feel that I am not utilizing all the features of delegates/events available in .NET 3.5 and beyond. And more or less still using delegates power available in 2.0. I would appreciate if you tell me how delegates/events should be used now, tricks, short-cuts.

Thanks.

EDIT

Jon suggested his publication on this subject, and I am posting here for easier navigations:

A few of my articles which may be relevant:

My book, C# in Depth, has a whole chapter devoted to delegates (chapter 5). Unfortunately that's not one of the free ones :(

ps. I couldn't find a duplicate of this question but did look through many on this theme.

+5  A: 

A few of my articles which may be relevant:

My book, C# in Depth, has a whole chapter devoted to delegates (chapter 5). Unfortunately that's not one of the free ones :(

The main changes in summary (as Jared said, these are language changes - .NET itself hasn't changed much beyond them becoming generic, and the framework supplying the handy Func and Action delegates):

C# 2:

  • Method group conversions:

    // Old:
    button.Click += new EventHandler(HandleClick);
    // New:
    button.Click += HandleClick;
    
  • Anonymous methods:

    button.Click += delegate { Console.WriteLine("Click!"); };
    
  • Covariance/contravariance:

    EventHandler generalHandler = LogEvent;
    button.Click += generalHandler;
    button.KeyPress += generalHandler; // Event type is KeyPressEventHandler
    

C# 3:

  • Lambda expressions:

    button.Click += (sender, args) => Console.WriteLine("Click!");
    

    or more importantly:

    var people = list.Where(person => person.Name != "Jon")
                     .OrderBy(person => person.Age);
    
Jon Skeet
Unfortunate for *us* you mean ;)
Andrew Hare
Well, that depends - if a teaser chapter ended up being enough to get someone to buy the book...
Jon Skeet
@Jon Skeet, thanks, I have all of them print out and read more than once... looking to dark corners....DARK!!!!
@Jon - A teaser chapter was enough for me :)
Andrew Hare
Care to elaborate your “old/new” distinction for method group conversion? I don't get it (or isn't it rather the other way round?).
Konrad Rudolph
NICE.... jon keep it coming
@Konrad: Yes, other way round.
Jon Skeet
@Sasha: I think I'm out of ideas for the minute. And I need to put the kids to bed :)
Jon Skeet
+1  A: 

Delegates didn't change at all 2.0 -> 3.5. What changed was a lot of items around them

  1. The System.Core.dll (finally) added reusable delegates in the form of Func<> and Action<>
  2. C# and VB gained a bit of a functional flavor with lambda expressions. This gave the languages very succinct syntax for expression a delegate operation. C# had anonymous methods in 2.0 but they just don't compare with the succint nature of lambda expressions

     list.Where(x => x > 42);
  3. LINQ in it's underpinnings exposes a series of APIs that operate purely on delegates.

So really delegetase haven't changed, just the uses.

JaredPar
Oh yea... this is great but I already know this, just don't use it...
+1  A: 

Have you considered using delegates for quick and dirty async operations? You can call the BeginInvoke and EndInvoke methods to perform async operations on the ThreadPool.

// Definition.
delegate string TransformDelegate(string input);

// Client code.  This is some lengthy operation.  You can also
// assign a function here if you want.
TransformDelegate t = (x) => ...;

// Begin invoking.
t.BeginInvoke("input", (ar) => 
{
    // Call end invoke.
    string result = t.EndInvoke(ar);

    // Dispose of wait handle, known issue documented here.
    // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94068
    using (IDisposable d = ar.WaitHandle) { }

    // Process end result here.  Remember, on another thread now.
}, null);
casperOne