相关疑难解决方法(0)

C#中的方法组是什么?

在以下情况下,我经常遇到一个错误,例如"无法从'方法组'转换为'字符串'"

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)

但是我想知道什么是方法组.谷歌也不是MSDN的帮助.

.net c# method-group

336
推荐指数
5
解决办法
15万
查看次数

简单的Linq表达式将无法编译

有这些基本定义

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的签名是明确的 - 它只需要一个(字符串)参数.

谁能在这里解决一些问题?

c# linq .net-4.0 .net-3.5

10
推荐指数
2
解决办法
1196
查看次数

标签 统计

c# ×2

.net ×1

.net-3.5 ×1

.net-4.0 ×1

linq ×1

method-group ×1