对于Func<>代表? [英] What's so great about Func<> delegate?

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

问题描述

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

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

问题:有没有什么伟大的Func<>它是5重载?从外观来看,我仍然可以用自己的说法创建一个类似的delgate,具有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.

eg: public delegate TResult MyFunc< TResult>()和各种重载的组合...

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<>并不是一个很好的例子。网络框架,我们可以使用,如果需要,创建自定义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


  • 动作 =只是做某些事情并返回没有输出的方法

  • 比较 =一些方法比较两个相同类型的对象,并返回一个int来表示顺序。

  • 转换器 =将对象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

我不必深入挖掘,除非是c的直接区域oncern。所以如果你感觉到你需要的代理人需要符合这些需求之一,那么在滚动你自己之前先使用它们。

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.ThreadStart over System.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天全站免登陆