开发者

When is it useful to define your own delegates instead of using the generics?

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?


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.


Hardly ever - basically when there is no generic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜