在以下情况下,我经常遇到一个错误,例如"无法从'方法组'转换为'字符串'"
var list = new List<string>();
// ... snip
list.Add(someObject.ToString);
Run Code Online (Sandbox Code Playgroud)
当然最后一行有一个拼写错误,因为我忘记了之后的调用括号ToString.正确的形式是:
var list = new List<string>();
// ... snip
list.Add(someObject.ToString()); // <- notice the parentheses
Run Code Online (Sandbox Code Playgroud)
有这些基本定义
bool MyFunc(string input)
{
return false;
}
var strings = new[] {"aaa", "123"};
Run Code Online (Sandbox Code Playgroud)
我想知道为什么这不会编译:
var b = strings.Select(MyFunc);
Run Code Online (Sandbox Code Playgroud)
但这会:
var c = strings.Select(elem => MyFunc(elem));
Run Code Online (Sandbox Code Playgroud)
该错误消息是"的类型参数方法'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable,System.Func)’不能从使用推断."
Resharper错误提示说它之间很困惑
Select(this IEnumerable<string>, Func<string, TResult>)
Run Code Online (Sandbox Code Playgroud)
和
Select(this IEnumerable<string>, Func<string, int, TResult>)
Run Code Online (Sandbox Code Playgroud)
...但MyFunc的签名是明确的 - 它只需要一个(字符串)参数.
谁能在这里解决一些问题?