使用Func键<> (或动作<>),或创建自己的代表?自己的、动作、代表、Func

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

哪一个更好的,可以说,在一个方法参数类型(不涉及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.

阅读全文

相关推荐

最新文章