C # notation of understanding Select (int.Parse)

I found a little script that I fully understand. For example, I have a line with "1 -2 5 40". It reads the input string, breaking it into a temporary array. Then this array is parsed, and each element is converted to an integer. The thing is to give the nearest integer to zero.

But I do not understand, this designation is Select (int.Parse) . There is no lambda expression here, and the int.Parse method is not called with parentheses. Same thing with OrderBy (Math.Abs)

Thanks in advance =)

var temps = Console.ReadLine().Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries);  

var result = temps.Select(int.Parse)
.OrderBy(Math.Abs)
.ThenByDescending(x => x)
.FirstOrDefault();
+6
source share
5 answers

int.Parse - - , , - . LINQ:

Func<string, int> parser = int.Parse;
int x = parser("10"); // x=10

:

Func<string, int> parser = text => int.Parse(text);

... , :)

+11

Select(int.Parse) Select(x => int.Parse(x)).

Select Func<T, R>, int.Parse ( ). .

Func<T, R> Func<string, int>, int Parse(string).

+5

int.Parse - → int (, , . , , , .

, .

0

.Select() Func<T1, T2>(), T1 - ( temps), T2 - .

, -: x => return x + 1 .. , , , .

So Func<string, int> parseInt = s => Convert.ToInt32(s); int.Parse(s).

Func .

0

LINQ IEnumerable < > :

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

selector. .Net int.Parse, :

public static int Parse(
    string s
)

The .Net compiler can convert delegates to Func <...> or Action <...>. In the case of int.Parse, it can be converted to Func and therefore can be passed as an argument to the Select method.

Similarly with OrderBy. Look at his signature.

0
source

All Articles