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)
| 归档时间: |
|
| 查看次数: |
1150 次 |
| 最近记录: |