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
  • 8,656
  • 3
  • 28
  • 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
  • 45,496
  • 53
  • 165
  • 281
2

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

Theodoros Chatzigiannakis
  • 27,825
  • 8
  • 68
  • 102
2

Check out the Action and Func delegates.

Femaref
  • 59,667
  • 7
  • 131
  • 173
2
var method =()=> DoSomething();
result = method();
Reza ArabQaeni
  • 4,796
  • 25
  • 44