无法从高阶函数的用法推断出类型参数

Gro*_*ozz 10 c# delegates functional-programming anonymous-delegates higher-order-functions

我有以下高阶函数:

public static Func<T, bool> Not<T>(Func<T, bool> otherFunc)
{
    return arg => !otherFunc(arg);
}
Run Code Online (Sandbox Code Playgroud)

并尝试这样称呼它:

var isValidStr = LinqUtils.Not(string.IsNullOrWhiteSpace);
Run Code Online (Sandbox Code Playgroud)

编译器给我"类型参数不能从使用中推断"错误.但以下工作:

var isValidStr = LinqUtils.Not((string s) => string.IsNullOrWhiteSpace(s));
Run Code Online (Sandbox Code Playgroud)

我想知道有什么区别? string.IsNullOrWhiteSpace已经是一个具有完全相同签名的非重载函数.

如评论中所述,以下内容也有效,但仍无法解释为什么在这种情况下类型推断失败:

var isValidStr = LinqUtils.Not<string>(string.IsNullOrWhiteSpace);
Run Code Online (Sandbox Code Playgroud)

pay*_*ayo 6

您正在处理的问题的细节由埃里克利珀在他的博客回答,在这里.

基本上,"大卫B"在你的评论说,"IsNullOrWhiteSpace是方法组.方法组只有一个今天参与成员,但它可能在未来得到更多."