Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a Function to Run Later

Tags:

function

c#

.net

I've got a loop that will need to "mark" functions to run later, after the loop is completed. Is that possible to do?

Thanks, Tyler

like image 916
Tyler Murry Avatar asked Dec 02 '22 05:12

Tyler Murry


1 Answers

You can store the function in a delegate:

private void Test(object bar)
{
    // Using lambda expression that captures parameters
    Action forLater = () => foo(bar);
    // Using method group directly
    Action<object> forLaterToo = foo;
    // later
    forLater();
    forLaterToo(bar);
}

private void foo(object bar)
{
    //...
}
like image 125
Quartermeister Avatar answered Dec 19 '22 03:12

Quartermeister