简而言之,关于 Func<> 可以说些什么?
我已经看到 Func 一段时间了,并且我已经设法避免它(暂时)。但现在看来,我是无法永远躲开了。例如,我尝试了 Dynamic Linq,但几乎所有内容都是根据 Func<> 进行的。我已经尝试过我的一本书(C# 2008/Deitel&Deitel)以及 MSDN,但我还没有得到它。他们都直接切入主题。
- 关于 Func<> 可以说些什么(用几句话来说)
- 我可以在网上找到一些可以帮助我开始处理此事的链接吗?
感谢您的帮助
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Func<>
是一个通用委托 - 使用起来非常方便,因为您不必为每个参数/返回类型组合创建自己的委托。早些时候,您必须编写如下内容:
您必须发布委托,以便用户可以正确调用您的方法。特别是当您需要一堆不同的委托时,您最终会为每个参数列表和返回类型发布一个委托。
使用
Func<>
您只需编写:它的含义与第一个代码示例相同 -
Func
定义一个委托,该委托接受一个整数参数并返回一个长值。当然,您也可以使用更长的参数列表:
Func
仍然会返回一个 long 值,而它需要两个 int< /em> 和一个 bool 值。如果您希望委托没有返回值,则必须使用Action<>
,它将 void 作为返回类型。编辑(根据请求):如何调用我的示例中的方法:
对于调用者来说,使用
MyDelegate
或功能
。在这两种情况下,他都有三个选项来调用方法:使用 lambda 表示法(需要 C# 3.0,可能是短方法的最佳解决方案):
使用匿名方法(需要 C# 2.0):
或者使用真实方法作为参数:
Func<>
is a generic delegate - it is just very convenient to use, because you don't have to create your own delegate for each argument/return type combination.Earlier, you had to write something like:
You had to publish your delegate so that a user can call your method correctly. Especially when you need a bunch of different delegates you ended up publishing one for every argument list and return type.
With
Func<>
you just write:It means the same as the first code example -
Func<int, long>
defines a delegate that takes one integer argument and returns a long value.Of course you can use longer parameter lists, too:
Func<int, int, bool, long>
will still return a long value while it takes two ints and a bool value. If you wish a delegate without return value you will have to useAction<>
, which will have void as a return type.EDIT (by request): How to call the method in my example:
For the caller, there is no difference between the solution with
MyDelegate
orFunc<>
. In both cases he has three options to call the method:Using a lambda notation (C# 3.0 required, probably the best solution for short methods):
By using an anonymous method (C# 2.0 required):
Or by using a real method as an argument:
Func<...>
是委托类型的家族,它返回一些值,并接受一定数量的参数;例如:Func
只是接受一个 int 并返回一个 bool 的东西(返回总是在最后);例如谓词:Func
是返回字符串的方法,例如() =>; "hello world";
.Func
可能类似于(when,howLong) =>; when + howLong;
同样,还有
Action<...>
,它执行相同的操作,但没有返回类型。Func<...>
没有什么神奇之处 - 它只是表达委托的一种更简单的方式,而 a:使用泛型(对 LINQ 有用),或 b:不需要您查找什么论据是;如果委托类型是模糊的(例如 PipeStreamImpersonationWorker ),则可能很难知道它需要什么;如果将其表示为类似的Action
,那么很明显它不带任何参数并返回void
。Func<...>
is a family of delegate types, that return some value, and take some number of arguments; for example:Func<int,bool>
is simply something that takes an int and returns a bool (the return is always at the end); for example a predicate:Func<string>
is a method that returns string, such as() => "hello world";
.Func<DateDtime, TimeSpan, DateTime>
might be something like(when,howLong) => when + howLong;
Likewise there is
Action<...>
which does the same but without a return type.There's nothing magic about
Func<...>
- it is just a simpler way of expressing delegates, while a: using generics (useful for LINQ), or b: not needing you to look up what the arguments are; if the delegate type is something obscure (PipeStreamImpersonationWorker
for example) it can be hard to know what it takes; if that was expressed as the comparableAction
it would be clear that it takes no parameters and returnsvoid
.这可能会有所帮助。假设每次看到
Func
时你都会想:也就是说,委托是一个实现了这个接口的对象。它有一个名为 Invoke 的方法,它接受一个 int 并返回一个字符串。
现在添加一个功能,您可以在呼叫中省略“调用”,这样您就拥有了一个代理。
This might help. Suppose every time you see
Func<int, string>
you think to yourself:That is, the delegate is an object that implements this interface. It has a single method called Invoke which takes an int and returns a string.
Now add to that the feature that you can omit the "Invoke" on a call, and you've got yourself a delegate.
Func
(例如)是一种类型(就像string
是一种类型一样)。所以你用它来声明变量、字段、参数等等。它表示每当您询问答案时都可以完成的计算:
请注意如何像调用方法一样调用它。
Func
有许多重载版本来支持不同数量的参数。最后一个类型参数是返回类型。Func
是委托类型。您可以声明自己的,但使用Func
更容易。在要返回void
的位置,请使用Action
而不是Func
。仅当需要out
或ref
参数时,才需要声明自定义委托。将 lambda 分配给
Func
时,您可以引用局部变量。这是极其强大的;这意味着Func
不仅仅是代码;它有数据。因此,它就像一个具有单个方法的对象(从技术上讲,该方法称为“Invoke”,并且当您调用委托时,编译器会隐式地为您调用该方法)。语法
() =>
可以放在任何表达式之前,表示“现在不要执行此操作,推迟到稍后执行”。它允许您初始化捕获延迟计算的委托。然后可以将语法()
放在委托后面以实际触发计算。所以后缀()
与前缀() =>
相反。Func<int>
(for example) is a type (in the way thatstring
is a type). So you use it to declare variables, fields, parameters and so on.It represents a computation that can be done whenever you ask it for an answer:
Note how you can call it just like a method. There are many overloaded versions of
Func
to support different numbers of parameters. The last type argument is the return type.Func
is a delegate type. You can declare your own, but it's easier to useFunc
. Where you want to returnvoid
, useAction
instead ofFunc
. You only need to declare custom delegates if you needout
orref
parameters.When assigning a lambda to a
Func
, you can refer to local variables. This is extremely powerful; it means that aFunc
is more than just code; it has data. So it's like an object with a single method (which technically it is - the method is calledInvoke
and the compiler implicitly calls that method for you when you call a delegate).The syntax
() =>
can be placed before any expression to say "don't do this now, delay it until later". It allows you to initialize a delegate capturing the delayed computation. And then the syntax()
can be placed after the delegate to actually trigger the computation. So suffix()
is kind-of the opposite of prefix() =>
.您可以从101 Linq 示例开始。
简而言之,
Func<>
是一个委托,其中最后一个类型参数是返回类型。因此,
Func
是一个接受int
参数并返回bool
的委托。You can start with 101 Linq Samples.
In short,
Func<>
is a delegate where the last type parameter is the return type.So,
Func<int,bool>
is a delegate that takes anint
parameter and returns abool
.函数<..., T>是代表。
其中 T 是返回类型,所有其他 - 输入参数。
Func<..., T> is delegate.
where T is return-type, and all others - input parameters.
如果您曾经使用过=>; C# 中的运算符,您可能已经使用过 Funcs。您只是没有明确声明它们。
因此,如果您编写一条语句,就像
将
Func
传递到Where() 方法中一样。如果你想啰嗦一些,你可以像这样重写该语句:
你会得到相同的结果。
If you've ever used the => operator in c#, and you probably have, you've already used Funcs. You just haven't been explicitly declaring them.
So, if you write a statement like
you're passing a
Func<Person, bool>
into the Where() method.If you want to be wordy, you can rewrite that statement like this:
And you'll get the same result.