c# - 为什么我不能只传递需要类似类型 Func 的方法的名称?

标签 c# mono gmcs

为什么这个 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/

相关文章:

cocoa - iPhone 风格 listViews for Cocoa/Monomac

c# - 作为服务的 Mono 编译器可以在调试上下文中使用吗?

c# - 使用不同文件夹中的 DLL 运行 mono .exe

c# - 让线程等待 n 个脉冲

c# - string.Contains 作为谓词而不是函数调用?

linux - 由于 Bison 设置问题,RHEL 5 中的单声道设置问题

c# - webkit-sharp for windows 包

c# - 根据用户角色动态创建菜单

c# - 如何将绘制的矩形存储在数组中?