Noop for Func<Task>?

Asked by At

There is a function

async Task Run(Func<Task> f)
    // do something
    await f();

Sometimes I need to call the function but don't have anything for parameter f so I pass it async () => { }. Is there a noop function for it?

await Run(async () => { }); // I still need to call Run

1 Answers

Mehrzad Chehraz On

The noop that you are looking for is Task.CompletedTask.

Gets a task that has already completed successfully.

However, you still need to pass a lambda since the method requires a Func<T> not a Task as an argument.

await Run(() => Task.CompletedTask);

This might not be that different from using a empty block as you suggested.