score:10

Accepted answer

Because it is an Extension Method.

So that instead of

IEnumerable<string> filteredNames = System.Linq.Enumerable.Where(names, n => n.Length >= 4);

You can also use it like:

IEnumerable<string> filteredNames = names.Where(n => n.Length >= 4);

The Reason it is an extension method is that IEnumerable, List, ... existed long before Linq (which was introduced in .Net 3.5) and its job is just to extend finding, filtering, ordering, ... them. So it is logical to have it as an Extention method rather than a separate library. And also consider that this way you can use chaining, which woudln'd be possible if it wasn't an extension:

name.Where(x => x.Length > 4).Select(x => x.Substring(4));

Compare it to:

System.Linq.Enumerable.Select(System.Linq.Enumerable.Where(name, x => x.Length > 4), x => x.Substring(4)); 

And this is only a very simple one, consider how dirty it gets with larger, complex queries.

score:2

Since it's an Extension Method. It means that Where is not a method on IEnumerable but when you reference Linq namespace ,Where method is added to IEnumerable.

for more info read this : Extension Methods

score:1

First of all, The simple answer is because it is an extension method. The definition of extension method is that Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Secondly, I disagree Ashkan Mobayen Khiabani's answers last part. you must have a look fluent implementation without extension method.


Related Articles