Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. However when I look at the documentation of these types I don't see any LINQ methods.

Are the LINQ methods extension methods that are inserted in IEnumerable/IQueryable types at runtime and if so why?

like image 508
Acaz Souza Avatar asked Aug 21 '11 13:08

Acaz Souza


1 Answers

Adding the methods on IEnumerable would mean that all types that derive from IEnumerable would have to implement them themselves. 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. 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.

The LINQ methods for Linq To Objects are defined on the Enumerable class.

Moreover, LINQ searches for methods that are declared using a specific syntax and is not interface or class based. You might find the Edulinq series of Jon Skeet interesting.

MSDN link on extension methods.

like image 148
BennyM Avatar answered Oct 06 '22 15:10

BennyM