Reputation: 421
Why can I invoke Parse method without parentheses since that method has 4 overloads?
For example in this case:
string[] aTemp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(aTemp, int.Parse);
Upvotes: 0
Views: 117
Reputation: 18105
The signature for ConvertAll
is actually this:
public static TOutput[] ConvertAll<TInput, TOutput>(
TInput[] array,
Converter<TInput, TOutput> converter
)
Which the compiler can infer to be:
public static int[] ConvertAll<string, int>(
string[] array,
Converter<string, int> converter
)
From the signature for Int32.Parse
:
public static int Parse(
string s
)
If you wanted to write out in long hand:
Converter<string, int> converter = new Converter<string, int>(Int32.Parse);
string[] aTemp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll<string, int>(aTemp, converter);
Note: Converter<TInput, TOutput>
is actually a delegate that takes as input a parameter of type TInput
and returns a value of type TOutput
.
Upvotes: 5
Reputation: 8986
Array.ConvertAll takes two parameters, an array of TInput, and a converter delegate from TInput to TOutput. There's only one overload of int.Parse that matches the signature of a converter delegate -
public static int Parse(
string s
)
Putting together all of the information available, we can pick the right method to call.
Upvotes: 0