Eug*_*kov 5 c# lambda type-inference
请考虑以下代码.出于这个原因,我想弄清楚C#编译器在使用lambda表达式时无法推断该函数Map返回Aw/o显式类型规范的类型.
这种情况的文件的任何理由或参考解释了这种行为?
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));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
179 次 |
| 最近记录: |