I have the following code:
public static class X
{
public static C Test<A,B,C>(this A a, Func<B,C> f)
where C:class
{
return null;
}
}
public class Bar
{
public Bar()
{
this.Test(foo); //this doesn't compile
this.Test((Func<int, string>)foo);
this.Test((int q) => "xxx");
}
string foo(int a) { return ""; }
}
Why doesn’t the marked line compile? Does it have something to do with return type not being part of the signature?
But the third line does compile, which makes me guess the compiler turns it into something similiar to the second line…
Basically, the type inference process described in section 7.5.2 of the spec is relatively weak when it comes to method group conversions. In the annotated standard, in section 7.5.2.6 which talks about Output Type Inferences – including method groups – there’s an annotation from Vladimir Reshetnikov stating:
I believe that’s exactly the problem here – sure, you’ve only actually got one method you can call and the method group only contains a single method, but the type inference process isn’t quite powerful enough to tie the two together.