使用 Func<> (或 Action<>)或创建自己的委托?

发布于 2024-10-08 01:37:53 字数 401 浏览 0 评论 0 原文

哪一个更好,比如方法中的参数类型(与 LINQ 无关)。 显然 Func 更好,因为它更简单,更具描述性,如果每个人都使用它,一切都会变得兼容(好)。 不过我注意到微软在一些库中使用了自己的委托,例如事件处理程序。那么,它们各自的优点和缺点是什么?我什么时候应该使用它?

编辑:

  • 显然是Func<>仅在 3.5 中可用,因此这可能是我看到非 Func 委托的主要原因。还有其他不使用 Func 的原因吗? (例如:来自.NET4)

  • 同样的问题适用于 Action<>

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?

Edits:

  • Apparently Func<> was only available in 3.5, so this can possible be the main reason that I saw non-Func delegates. Any other reason to not use Func? (example: this is from .NET4)

  • The same question also applies for Action<>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陈年往事 2024-10-15 01:37:53

函数<>当非常清楚它们的用途并且输入数量很少时,它很有用。

当输入数量较大时,或者意图可能存在一些模糊性时 - 那么使用带有命名参数的委托会让事情变得更清晰。

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.

三生一梦 2024-10-15 01:37:53

它们在所有用途上都是相同的,除非该方法具有 Expression 参数。这些需要被定义为“lambda”而不是delegate。当处理IQueryable并调用/解析IEnumerable时(例如LINQ2SQL),这将是非常有问题的。

They are for all purposes the same, except when the method has an Expression parameter. Those need to be defined as a 'lambda' and not delegate. This would be very problematic when dealing with IQueryable and getting the IEnumerable invoked/resolved instead (eg LINQ2SQL).

原来是傀儡 2024-10-15 01:37:53

函数<>和动作>>如果它们确实适合您的情况,则更可取。
运行时会创建程序中使用的每种类型的实例,如果您使用 Func 一次,则意味着该类型的实例已创建。在自定义委托的情况下,事情会以不同的方式进行,并且将创建新类型,即使它们本质上与现有类型相似。

然而有时自定义委托会使代码更清晰。

Func<> and Action<> are preferable if they are actually appropriate in your case.
Runtime creates instances of every type used in your program, and if you use Func once it means that the instance of type was created. In case of custom delegate things go in different way and new types will be created even though they are essentially similar to existing.

However sometimes custom delegates make code clearer.

骄兵必败 2024-10-15 01:37:53

但是我注意到 Microsoft 在某些库中使用自己的委托,例如事件处理程序。那么,它们各自的优点和缺点是什么?我什么时候应该使用它?

其中一些是历史性的:在 C#3/.NET3 之前添加 ActionFunc 时定义的 API。对于事件 EventHandler 是更好的选择,因为它强制执行正确的约定。

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?

Some of this is historic: APIs defined before C#3/.NET3 when Action<> and Func<> were added. For events EventHandler<T> is a better choice for events because it enforces the right convention.

扶醉桌前 2024-10-15 01:37:53

您始终可以创建一个类,该类将 n 个输入保存为类属性,并将该类的对象传递给 func<> 中的单个输入。代表

You can always create a class which holds the n number of input as class properties and pass the object of the class a single input in the func<> delegate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文