Why doesn't adding a return type prevent me from using method group syntax?

I am trying to use a group of methods in a lambda expression, for example:

public class Foo { public void Hello(string s) { } }

void Test()
{
    // this works as long as Hello has a void return type
    Func<Foo, Action<string>> a = foo => foo.Hello;
}

When I change the return type Helloto int, however, I get

'Bar.Hello (string)' has the wrong return type.

I tried playing with Funcinstead Action, but this prevents me from using the method group syntax.

Any ideas?

(My goal, fwiw, is to be able to reference multiple methods that have different return types and many string arguments. I'm not even going to call them - I just want to reflect their attributes. Like lambda security, however, instead of just entering method name strings.)


. Action<string>: int, . -

void Set<T>(Func<Foo, Func<string, T>> a) { }
void Test() { Set(foo => foo.Hello); }  // fails

- T ( , ?).

? - , .

+3
2

, Action<string>. , :

int Foo(string s) { return 10; }

// Error: wrong return type
Action<string> action = new Action<string>(Foo);

, , . Eric Lippert " " .

:

public class Foo { public int Hello(string s) { return 10; } }

void Test()
{
    Func<Foo, Func<string, int>> a = foo => foo.Hello;
}

VS2008, VS2010. # 4 - , , , .

+7

void foo.Hello Action<string>. int Func<string, int>.

- , -void :

Func<Foo, Action<string>> a = foo => s => foo.Hello(s);
+6

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


All Articles