代码之家  ›  专栏  ›  技术社区  ›  David Siegel

为什么我不能只传递一个方法的名称,而这个方法需要一个类似类型的Func?

  •  4
  • David Siegel  · 技术社区  · 15 年前

    为什么不检查一下?在这个例子中,我试图传递一个类型为 string -> string 作为一个 Func<string, string>

    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";
      }
    }
    
    1 回复  |  直到 15 年前
        1
  •  6
  •   Community CDub    8 年前

    您可以使用C#4编译器。C#3编译器对方法组转换的类型推断较弱。您可以在中阅读详细信息 Eric Lippert's answer here . 我还不完全清楚这是否意味着C#3编译器实际上没有实现C#3规范,或者规范本身是否在这方面在3和4之间发生了变化。这是一个相当学术的问题相比,编译器是否做你想做的 希望 将其发送给;)

    推荐文章