今天,我终于得到的 Func键<&GT ;
委托,看到我怎么可以用它来使我的一些难读的LINQ查询(希望)更具可读性。
Today I finally "got" the Func<>
delegate and saw how I could use it to make some of my less readable LINQ queries (hopefully) more readable.
下面是一个简单的code示例说明以上情况,在一个(非常)简单的例子
Here's a simple code sample illustrating the above, in a (very) trivial example
List<int> numbers = new List<int> { 1, 5, 6, 3, 8, 7, 9, 2, 3, 4, 5, 6, };
// To get the count of those that are less than four we might write:
int lessThanFourCount = numbers.Where(n => n < 4).Count();
// But this can also be written as:
Func<int, bool> lessThanFour = n => n < 4;
int lessThanFourCount = numbers.Where(lessThanFour).Count();
能否别人给的,他们使用的情况下任何的例子 Func键&LT;&GT;
(请注意,我不主张使用 Func键&LT;&GT;
方案中的那样简单如上图所示,这只是一个例子,希望使的功能 Func键&LT;&GT;
明确)
(Note that I would not advocate using Func<>
in a scenario as simple as that shown above, it's just an example that hopefully makes the functionality of Func<>
clear.)
我猜只会做这个,如果你将要重用Func键问题在许多地方的一个点(这涉及到多琐碎的逻辑)。否则,使用标准的方式似乎要好得多,完全可读。
I guess there would only be a point in doing this if you were going to be reusing the Func in question in a number of places (and it involved more than trivial logic). Otherwise using the standard way seems much better and perfectly readable.