Why can't the C # compiler deduce the type of a lambda expression with dynamic?

Consider the code below. For reasons I would like to find out, the C # compiler was unable to conclude that the Map function returns type A with an explicit type specification when using the lambda expression.

Any reasons or documentation links for such a case explaining 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)); } } 
+5
source share

Source: https://habr.com/ru/post/1266152/


All Articles