简体   繁体   English

LINQ方法是扩展方法?

[英]LINQ Methods are Extension Methods?

I want to understand LINQ and did some research on the matter and discovered that IEnumerable and IQueryable have LINQ methods. 我想了解LINQ并对此事做了一些研究,发现IEnumerableIQueryable都有LINQ方法。 However when I look at the documentation of these types I don't see any LINQ methods. 但是,当我查看这些类型的文档时,我没有看到任何LINQ方法。

Are the LINQ methods extension methods that are inserted in IEnumerable/IQueryable types at runtime and if so why? 是LINQ方法在运行时插入IEnumerable/IQueryable类型的扩展方法,如果是这样,为什么?

Adding the methods on IEnumerable would mean that all types that derive from IEnumerable would have to implement them themselves. 在IEnumerable上添加方法意味着从IEnumerable派生的所有类型都必须自己实现它们。 Now with extensions methods we can sort of add implementations to interfaces. 现在使用扩展方法,我们可以添加实现到接口。

An other side effect would be that code written in .NET 2.0 would still compile with 3.0 when Linq was introduced. 另一个副作用是,当引入Linq时,用.NET 2.0编写的代码仍然可以用3.0编译。 Otherwise if you had implemented IEnumerable somewhere in your project you would now have to implement all the new methods in the interface as well. 否则,如果您在项目中的某个位置实现了IEnumerable,那么现在还必须在接口中实现所有新方法。

The LINQ methods for Linq To Objects are defined on the Enumerable class. Linq To Objects的LINQ方法在Enumerable类上定义。

Moreover, LINQ searches for methods that are declared using a specific syntax and is not interface or class based. 此外,LINQ搜索使用特定语法声明的方法,而不是基于接口或类。 You might find the Edulinq series of Jon Skeet interesting. 您可能会发现Jon Skeet的Edulinq系列很有趣。

MSDN link on extension methods. 关于扩展方法的MSDN链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM