C# generic delegate type inference

2019-07-04 11:24发布

问题:

Why can't the C# compiler infer T to int in the specified example?

void Main()
{
    int a = 0;
    Parse("1", x => a = x);
    // Compiler error:
    // Cannot convert expression type 'int' to return type 'T'
}

public void Parse<T>(string x, Func<T, T> setter)
{
    var parsed = ....
    setter(parsed);
}

回答1:

Method type inference on a lambda requires that the types of the lambda parameters be already known before the types of the returns are inferred. So for example if you had:

void M<A, B, C>(A a, Func<A, B> f1, Func<B, C> f2) { }

and a call

M(1, a=>a.ToString(), b=>b.Length);

then we would infer:

A is int, from the first argument
Therefore the second parameter is Func<int, B>. 
Therefore the second argument is (int a)=>a.ToString();
Therefore B is string.
Therefore the third parameter is Func<string, C>
Therefore the third argument is (string b)=>b.Length
Therefore C is int.
And we're done.

See, we need A to work out B, and B to work out C. In your case you want to work out T from... T. And you can't do that.



回答2:

See http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx section on generic methods.

Note that the compiler cannot infer the type based on the type of the returned value alone.