![](/img/trans.png)
[英]In LINQ, how do I modify an existing LINQ extension method to add a “By” selector, i.e. add Func<T> TResult to the function signature?
[英]How do I correlate a Linq query extension method to its signature in the documentation?
从MSDN中获取以下代码:
下面的代码示例演示如何使用Where(IEnumerable,Func)筛选序列。
List<string> fruits =
new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);
foreach (string fruit in query)
{
Console.WriteLine(fruit);
}
/*
This code produces the following output:
apple
mango
grape
*/
当我看签名时
Where<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)
(fruit => fruit.length < 6)
哪一部分是IEnumerable<TSource>
? Func<TSource, Boolean>
包含整个lambda或仅包含=>
之后的内容? 我猜测幕后的<TSource>
被具有正确的Generic类型的编译器替换了,但是我不知道如何阅读其余内容。
编辑:就看文档中的内容是什么,是否更容易理解这是否是委托而不是lambda?
如果查看方法签名,则会看到它定义为
public static Enumerable
{
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
}
this
使其成为扩展方法。 这样做
fruits.Where(fruit => fruit.Length < 6);
和做完全一样
Enumerable.Where(fruits, fruit => fruit.Length < 6);
因此,要回答您的问题, IEnumerable<T>
在的左侧.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.