为什么这个 C# 不进行类型检查?在这个例子中,我试图传递一个类型为 string -> string
的方法。作为Func<string, string>
.在仅传递适当类型的函数的名称时能够省略 lambda 语法似乎是完全合理的。
using System;
using System.Linq;
class WeakInference
{
public static void Main (string [] args)
{
// doesn't typecheck
var hellos = args.Select (AppendHello);
// have to do this:
// var hellos = args.Select (s => AppendHello (s));
}
static string AppendHello (string s)
{
return s + "hello";
}
}
最佳答案
您可以使用 C# 4 编译器。 C# 3 编译器在方法组转换方面的类型推断较弱。您可以在 Eric Lippert's answer here 中阅读详细信息.我不完全清楚这是否意味着 C# 3 编译器实际上没有实现 C# 3 规范,或者规范本身是否在该区域的 3 和 4 之间发生了变化。与编译器是否执行您想要它做的事情相比,这是一个相当学术的问题;)
(我刚刚测试过,你的程序不能用 VS 2008 编译,但是可以用 VS 2010 编译。)
关于c# - 为什么我不能只传递需要类似类型 Func 的方法的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3202114/