Why can't I pass only the name of the method in which Func is entered similarly?

Why is this C # typecheck? In this example, I am trying to pass a method like string -> string as Func<string, string> . It would be perfectly reasonable to be able to ignore the lambda syntax when passing only the name of the corresponding typed function.

 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"; } } 
+4
source share
1 answer

You can use the C # 4 compiler. The C # 3 compiler had weaker type inference around method group conversions. You can read the details in Eric Lippert's answer here . It’s not entirely clear to me whether this means that the C # 3 compiler does not actually implement the C # 3 specification, or whether the specification itself has changed between 3 and 4 in this area. This is a pretty academic question compared to whether the compiler does what you want;)

(I just tested it, and your program does not compile with VS 2008, but compiles with VS 2010.)

+6
source

Source: https://habr.com/ru/post/1315043/


All Articles