2

I found this post that explains how to pass methods as parameters in C#.

What I need to know is how to return a method as the result of another method invocation.

method = DoSomething()
result = method()
Community
  • 1
  • 1
Apalala
  • 9,017
  • 3
  • 30
  • 48

4 Answers4

4

you need to use either Action<T> or Func<T>

Like this:

private Action<string> Returns(string user)
{
    return () => 
    {
        Console.WriteLine("Hey {0}", user);
    };
}

or this:

private Func<bool> TestsIsThirty(int value)
{
    return () => value == 30;
}
bevacqua
  • 47,502
  • 56
  • 171
  • 285
2

Most probably you want your return type to be a Delegate.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
2

Check out the Action and Func delegates.

Femaref
  • 60,705
  • 7
  • 138
  • 176
2
var method =()=> DoSomething();
result = method();
Reza ArabQaeni
  • 4,848
  • 27
  • 46