Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This is declaration of Select method:
Select<tsource,tresult>(IEnumerable<tsource>, Func<tsource,int32,tresult>)

So why we use it as so:
list.Select(s=>s.Name)

s=>s.Name is an anonymous method so it is a Func delegate then where is the first parameter which is IEnumerable<tsource>

I would expect it
list.Select(list,s=>s.Name)

What I have tried:

I searched on linq materials, and searched on google.
Posted
Updated 16-Aug-19 2:38am

1 solution

Select is an extension method with the first param (IEnumerable<tsource> source) marked as "this";

IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)


The way extension methods word is the object you enact them on is passed to the "this" param so you're right in that the method is actually called;

list.Select(list,s=>s.Name)



however .net passes the "list" object as the first param as that param is marked "this"

so this code here;

list.Select(s=>s.Name)


is compiled as

list.Select(list,s=>s.Name)


with the compiler passing the first param for you.
 
Share this answer
 
Comments
OriginalGriff 16-Aug-19 8:41am    
:thumbsup:
Spot on - +5!
Richard Deeming 16-Aug-19 14:21pm    
Nit-picking: It's compiled as Enumerable.Select(list, ..., not list.Select(list, ... :)
BillWoodruff 18-Aug-19 8:32am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900