Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# notation understanding Select(int.Parse)

I found a little script that I understand fully. I've got a string with "1 -2 5 40" for example. It reads the input string, splits it into a temporary array. Then this array is parsed and each element is transformed into an integer. The whole thing is order to give the nearest integer to zero.

But what I don't understand is the notation Select(int.Parse). There is no lambda expression here and the method int.Parse isn't called with brackets. Same with the OrderBy(Math.Abs)

Thank you in advance =)

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

var result = temps.Select(int.Parse)
.OrderBy(Math.Abs)
.ThenByDescending(x => x)
.FirstOrDefault();
like image 975
Angecroft Avatar asked Jun 08 '17 11:06

Angecroft


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

int.Parse is a method group - what you're seeing is a method group conversion to a delegate. To see it without LINQ:

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

It's mostly equivalent to:

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

... although there are plenty of differences if you want to go into the details :)

like image 183
Jon Skeet Avatar answered Oct 13 '22 14:10

Jon Skeet


Select(int.Parse) is nearly equivalent to Select(x => int.Parse(x)).

The Select demands an Func<T, R>, which in this case is also the signature of int.Parse (it has a single parameter with a return value). It convers the method group to the matching delegate.

In this case Func<T, R> will map to Func<string, int>, so it matches the int Parse(string) signature.

like image 41
Patrick Hofman Avatar answered Oct 13 '22 16:10

Patrick Hofman