It might help to look at the definition of this method in C#, from the MSDN article you refer to:
public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector
)
The <
angle brackets>
denote the type parameters for this generic method, and we can start to explore the purpose of the method simply by looking at what the type parameters are doing.
We begin by looking at the name of the generic method:
Select<TSource, TResult>
This tells us that the method called Select
deals with two different types:
- The type
TSource
; and
- The type
TResult
Let's look at the parameters:
- The first parameter is
IEnumerable<TSource> source
— a source, providing a TSource
enumeration.
- The second parameter is
Func<TSource, TResult> selector
— a selector function that takes a TSource
and turns it into a TResult
. (This can be verified by exploring the definition of Func
)
Then we look at its return value:
IEnumerable<TResult>
We now know this method will return a TResult
enumeration.
To summarise, we have a function that takes an enumeration of TSource
, and a selector function that takes individual TSource
items and returns TResult
items, and then the whole select function returns an enumeration of TResult
.
An example:
To put this into concrete terms, lets say that TSource
is of type Person
(a class representing a person, with a name, age, gender, etc), and TResult
is of type String
(representing the person's name). We're going to give the Select
function a list of Person
s, and a function that, given a Person
will select just their name. As the output of calling this Select
function, we will get a list of String
s containing just the names of the people.
Aside:
The last piece of the puzzle from the original method signature, at the top, is the this
keyword before the first parameter. This is part of the syntax for defining Extension Methods, and all it essentially means is that instead of calling the static Select method (passing in your source enumeration, and selector function) you can just invoke the Select method directly on your enumeration, just as if it had a Select
method (and pass in only one parameter — the selector function).
I hope this makes it clearer for you?