C# generic delegate type inference

亡梦爱人 提交于 2020-01-04 05:15:13

问题


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.



来源:https://stackoverflow.com/questions/9325488/c-sharp-generic-delegate-type-inference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!