如何将Linq查询扩展方法与文档中的签名相关联?

时间:2017-01-27 21:45:56

标签: c# linq extension-methods

MSDN获取此代码:

  

以下代码示例演示了如何使用   其中(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?

1 个答案:

答案 0 :(得分:3)

如果查看方法签名,您会看到它定义为

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>位于.

的左侧
相关问题