I am having trouble understanding the reason why the following code gives me an error:
var funs = Enumerable.Range(0, 10).Select(x => (int y) => x + y); foreach (var fun in funs) Console.WriteLine("{0}", fun(10));
Error: βAn implicitly typed declaration of a local variable cannot be initialized usingβ System.Collections.Generic.IEnumerator.Current. βI know how to fix this (either by selecting the type to select, for example, Select<int, Func<int, int>> , or using a helper method such as private static Func<T1, TR> MakeFunc<T1, TR>(Func<T1, TR> f) { return f; } and using Select(x => MakeFunc(y => x + y)) .
However, I would like to understand the reason why the compiler cannot deduce types. So far, I guess, according to 7.15.6, he cannot figure out if he should translate the inner lambda to Func or Expr. Is it right or is there something else?
For reference, here is what 7.15.6 says:
βAn anonymous function F must always be converted to a delegate type D or an expression tree of type E either directly or through the execution of the delegate creation expression new D (F). This conversion determines the result of the anonymous function.β
source share