使用Func键<> (或动作<>),或创建自己的代表? [英] use Func<> (or Action<>) or create own delegate?

查看:199
本文介绍了使用Func键<> (或动作<>),或创建自己的代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个更好的,可以说,在一个方法参数类型(不涉及LINQ)。 显然,func是更好,因为它更简单,更具描述性的,如果每个人都用这个一切都将变得兼容(好)。 不过,我注意到微软使用自己的代表在一些图书馆,例如事件处理程序。因此,什么是其中一方的优点和缺点?当我应该使用它?

Which one is better in, say, parameter type in a method (not related to LINQ). Apparently Func is better since it's simpler, more descriptive, and if everyone is using this everything will become compatible (good). However I notice Microsoft uses its own delegate at some libraries, for example event handlers. So, what are the advantages and drawbacks of either of them? when should I use it?

编辑:

  • 显然Func键<>仅在3.5可用,所以这可能是可能的,我看到非Func键代表的主要原因。其他任何理由不使用Func键? (例如:是从.NET4)

同样的问题也适用于动作<>

The same question also applies for Action<>

推荐答案

Func键&LT;>是有用的,当它是很清楚他们是用来做什么,以及输入数量比较少。

Func<> is useful when it's very clear what they're used for, and the number of inputs is small.

在输入数量较大,或者有可能是一些模糊过的意图 - 然后使用名为参数的委托使事情更加清楚。

When the number of inputs is larger, or there could be some ambiguity over the intent - then using a delegate with named arguments makes things clearer.

这篇关于使用Func键&LT;&GT; (或动作&LT;&GT;),或创建自己的代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆