Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Higher-order function returning function returning nothing

Tags:

c#

In C# how do I define function which return function which returns nothing? Something like this:

class X
{
    public Func<void> GetFuncReturningVoid() { ... }
}
like image 826
Lavir the Whiolet Avatar asked Aug 07 '26 23:08

Lavir the Whiolet


1 Answers

A function returning nothing is an Action. Using a lambda expression, you could write this:

Action GetFuncReturningVoid() {
    return () => Console.Writeline("my action");
}

And if you need to accept arguments...

Action<int, int> GetActionWithArguments() {
    return (int x, int y) => Console.Writeline(x * y);
}

Or you can let the compiler infer the types:

Action<int, int> GetActionWithArguments() {
    return (x, y) => Console.Writeline(x * y);
}
like image 109
dahlbyk Avatar answered Aug 10 '26 13:08

dahlbyk