Apalala
Apalala

Reputation: 9224

Returning a method from a C# method

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()

Upvotes: 2

Views: 216

Answers (4)

bevacqua
bevacqua

Reputation: 48476

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;
}

Upvotes: 4

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4907

var method =()=> DoSomething();
result = method();

Upvotes: 2

Femaref
Femaref

Reputation: 61427

Check out the Action and Func delegates.

Upvotes: 2

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29213

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

Upvotes: 2

Related Questions