C# compiler can correctly infer type of s (string) in these snippets:
Func<int, string, string> f1 = (n, s) => s.Substring(n);
Func<int, Func<string, string>> f2 = n => s => s.Substring(n);
But it can't in this one [1]:
var numbers = Enumerable.Range(1, 10);
IEnumerable<Func<string, string>> fs = numbers.Select(n => s => s.Substring(n));
To make it work one has to do something like this:
var fs = numbers.Select(n => new Func<string, string>(s => s.Substring(n));
or
var fs = numbers.Select(f2);
And the question is - why type inference doesn't work in [1] if all needed information about types is known beforehand?
All the information about the type isn't known beforehand. In your first working snippet, you're telling it on both lines which delegate type you want s => s.Substring(n)
to be converted to.
In your second snippet, the only place where that information is present is in the assignment of the result of Select
... and that isn't used as part of overload and type inference when the compiler is working out what the Select
call itself means.
So the options are:
f2
Select
call, or use a new
operator as per your snippetProvide the type arguments for Select
directly:
var fs = numbers.Select<int, Func<string, string>>(n => s => s.Substring(n));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With