tags:

views:

34

answers:

2

I've been going through some old code, where I came across some custom defined delegates, which are used thus:

    private delegate void ListenDelegate(UdpClient listener, bool multicast);
    private void ListenOn(UdpClient listener, bool multicast)
    {
        new ListenDelegate(_ListenLoop).BeginInvoke(listener, multicast, null, null);

    }

With some of the new .NET framework versions, you can do the following:

    private void ListenOn(UdpClient listener, bool multicast)
    {
        new Action<UdpClient, bool>(_ListenLoop).BeginInvoke(listener, multicast, null, null);
    }

This ought to be exactly the same. Is there any point in defining your own delegates, when the generic delegates seem to do the same job with less space? Or have I missed something about the generics that makes them not equivalent?

+1  A: 

Hardly ever - basically when there is no generic.

TomTom
+1  A: 

I tend to use them when there are many generics nested and the generic delegate becomes quite unreadable. Or when parameter names cannot be easily inferred by the reader. An Action<string, string, string> tells nothings about what it wants if there are no parameter names.

Julien Lebosquain
Remember, delegates were invented before generics. If generics had come first, delegates probably wouldn't have names; instead, delegates would probably be a single generic type (e.g. delegate<int,void>) and named delegates probably would have merely served as aliases, or places to put documentation.
Qwertie