Consider the code below. For the reasons I'd like to figure out C# compiler unable to infer that function Map
returns type A
w/o explicit type specification when lambda expression is used.
Any reasons or references to documentation for such case that explain the behavior?
class Program
{
public class A
{
}
public static A Map(dynamic x)
{
return new A();
}
static void Main(string[] args)
{
IEnumerable<dynamic> d = new dynamic[] { };
// OK
IEnumerable<A> a1 = d.Select(Map);
// OK
IEnumerable<A> a2 = d.Select(x => (A)Map(x));
// NOT OK
// Cannot implicitly convert type 'IEnumerable<dynamic>' to 'IEnumerable<A>'
IEnumerable<A> a3 = d.Select(x => Map(x));
}
}