It was not entirely clear how to name this question, so I would appreciate it if they correct the topic.

There is a function for getting only non-empty strings from an array.

public static int[] ParseNumbers(IEnumerable<string> lines) { return lines .Where(x => x != "") .Select(int.Parse) .ToArray(); } 

Why in Select(int.Parse) and not Selext(x=>x) ? Although this code works without problems, it is not entirely clear how it works.

  • 2
    Select(x=>x) returns IEnumerable<string> and after calling ToArray result is an array of strings, and the function needs to return an array of integers, so the function int.Parse - Grundy is applied to each element
  • You can always write a complete form if this one confuses you. x => int.Parse(x) and that's it. - Monk
  • @Monk, this is not the complete form :-) this is the function inside which the function is called. - Grundy
  • @Grundy is it compiled into different instructions? - Monk
  • @Monk, depends on the compiler, maybe in some, maybe in different ones - Grundy

1 answer 1

int.Parse is called a group of methods (a group can consist of one method or several, if there are overloaded versions). A group of methods can be converted (according to 6.6 of the C # specification) into a compatible delegate. Because Select accepts Func<Result, Arg> , and int.Parse has the following signature: static int Parse(string s) , then it (the group) is converted to the delegate Func<int, string> which is called inside Select

  • oh how complicated it is ( - Rajab
  • @Radzhab, on the contrary, it's all very simple. If you were dealing with C ++, then this is something like function pointers. - ixSci