Func 有什么了不起的地方<>代表? [英] What's so great about Func<> delegate?

查看:15
本文介绍了Func 有什么了不起的地方<>代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是基本的,但我正在尝试使用 .Net 3.5.

Sorry if this is basic but I was trying to pick up on .Net 3.5.

问题:Func<> 有 5 个重载吗?从外观上看,我仍然可以自己创建一个类似的委托,例如 MyFunc<>,具有确切的 5 个重载甚至更多.

Question: Is there anything great about Func<> and it's 5 overloads? From the looks of it, I can still create a similar delgate on my own say, MyFunc<> with the exact 5 overloads and even more.

例如:public delegate TResult MyFunc() 和各种重载的组合...

eg: public delegate TResult MyFunc<TResult>() and a combo of various overloads...

这个想法是在我试图理解 Func<> 委托时产生的,并遇到了以下场景:

The thought came up as I was trying to understand Func<> delegates and hit upon the following scenario:

Func<int,int> myDelegate = (y) => IsComposite(10);

这意味着委托具有一个 int 类型的参数和一个 int 类型的返回类型.有五种变体(如果您通过智能感知查看重载).所以我猜我们可以有一个没有返回类型的委托?

This implies a delegate with one parameter of type int and a return type of type int. There are five variations (if you look at the overloads through intellisense). So I am guessing that we can have a delegate with no return type?

那么我是否有理由说 Func<> 没什么了不起,只是 .Net 框架中的一个示例,我们可以使用,如果需要,创建自定义func<>"委托以满足我们自己的需要?

So am I justified in saying that Func<> is nothing great and just an example in the .Net framework that we can use and if needed, create custom "func<>" delegates to suit our own needs?

谢谢,

推荐答案

伟大之处在于建立共享语言以更好地沟通.

不要为同一事物定义自己的委托类型(委托爆炸),而是使用框架提供的委托类型.任何阅读您的代码的人都会立即掌握您要完成的工作.. 最大限度地减少这段代码实际上在做什么?"的时间.所以当我看到一个

Instead of defining your own delegate types for the same thing (delegate explosion), use the ones provided by the framework. Anyone reading your code instantly grasps what you are trying to accomplish.. minimizes the time to 'what is this piece of code actually doing?' So as soon as I see a

  • Action = 一些只做某事但不返回输出的方法
  • Comparison = 比较相同类型的两个对象并返回一个 int 来指示顺序的方法
  • 转换器 = 将 Obj A 转换为等效的 Obj B
  • EventHandler = 给定事件参数形式的某些输入,对某个对象引发的事件的响应/处理程序
  • Func = 某种方法,接受一些参数,计算一些东西并返回一个结果
  • 谓词 = 根据某些标准评估输入对象并将通过/失败状态返回为 bool
  • Action = some method that just does something and returns no output
  • Comparison = some method that compares two objects of the same type and returns an int to indicate order
  • Converter = transforms Obj A into equivalent Obj B
  • EventHandler = response/handler to an event raised by some object given some input in the form of an event argument
  • Func = some method that takes some parameters, computes something and returns a result
  • Predicate = evaluate input object against some criteria and return pass/fail status as bool

除非这是我直接关注的领域,否则我不必深入挖掘.因此,如果您觉得您需要的代表符合其中一项需求,请在推出自己的委托之前使用它们.

I don't have to dig deeper than that unless it is my immediate area of concern. So if you feel the delegate you need fits one of these needs, use them before rolling your own.

免责声明:我个人喜欢语言设计者的这一举动.

反论点:有时定义代理可能有助于更好地传达意图.例如System.Threading.ThreadStartSystem.Action 之上.所以说到底是个判断.

Counter-argument : Sometimes defining your delegate may help communicate intent better. e.g. System.Threading.ThreadStart over System.Action. So it’s a judgment call in the end.

这篇关于Func 有什么了不起的地方&lt;&gt;代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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