views:

151

answers:

1

Is there any best practice with respect to coding style with respect to explicit use of the delegate keyword instead of using a lambda?

e.g.

new Thread(() =>
{
 // work item 1
 // work item 2
}).Start();

new Thread(delegate()
{
 // work item 1
 // work item 2
}).Start();

I think the lambda looks better. If the lambda is better style, what's the point of having a delegate keyword, other than for the fact that it existed before lambdas were implemented?

+1  A: 

Lambda syntax is much more generalised, and the designers have said that they'd ideally remove the old overlapping syntaxes (dont have a citation, but it's probably Eric Lippert or Jon Skeet in a book or a podcast).

But delegate allows you to ignore parameters, e.g.:

object.Event += delegate { };

versus having to say:

object.Event += (sender,args) => { };

which can be very useful in large argument lists and/or to make the code more resilient to refactoring.

EDIT: As pointed out by Yann Schwartz in another answer (now unfortunately deleted), a very neat usage of this trick is in order to provide a default hander for an event using the Null Object pattern:-

class MyClassThatFiresWithoutTheTrick
{
    public event EventHandler MyEvent; // implicit = null

    // Need a method to keep this DRY as each fire requires a null check - see Framework Design Guidelines by Abrams and Cwalina
    protected virtual void OnMyEvent()
    {
        // need to take a copy to avoid race conditions with _removes
        var handler = MyEvent;
        // Need to do this check as it might not have been overridden
        if( handler == null)
            return;
        handler( this, EventArgs.Empty );
    }
}

class MyClassThatFiresWithTheTrick
{
    public event EventHandler MyEvent = delegate{};

    protected virtual void OnMyEvent()
    {
        MyEvent( this, EventArgs.Empty );
    }
}

(though what you might often end up doing is an Inline Method of OnMyEvent, making the code even shorter again.)

Ruben Bartelink
I did not add this as a comment [I love your passive agressive way of stating it in an aside, btw] was because I actually answered before you did. Sorry for not having a time machine handy.
Yann Schwartz
Well, to be exact, I hit submit while you were answering. HTH.
Yann Schwartz
"Lambda syntax is much more generalised, and the designers have said that they'd ideally remove the old overlapping syntaxes (dont have a citation, but it's probably Eric Lippert or Jon Skeet in a book or a podcast)." - That's what I was looking for, cheers.
Nathan Ridley
@Yann: I should be more mature and will remove all references to this nonsense. But if you look at the answers, mine was posted 2 mins before yours and didnt add much. When that happens to me, I delete my answer. My passive agressiveness was triggered by this, sorry.
Ruben Bartelink
@Yann: Before I delete the EDIT: section and fighting comments, can you please add a reference to the Null Object pattern into your answer and we can close this off? I'll even chuck in a +1 :P
Ruben Bartelink
@Ruben. Alrighty :)
Yann Schwartz
@Yann: You were supposed to edit into your answer and me +1 it! If you're bothered, feel free to Extract Answer everything below the *EDIT:* marker.
Ruben Bartelink
@Yann No it's ok really. No need for duplicate answers since yours has it all now.
Yann Schwartz
Well, *ideally* we would have gotten it perfect in C# 2, and not ended up with two syntaxes to do the same thing in C# 3. I personally never use the anonymous method ("delegate") syntax unless I'm writing code that has to be portable to C# 2.
Eric Lippert