Your method could functionally be condensed down to this.
public static Task F()
{
return Task.Delay(1000);
}
Returning an Task.CompletedTask
instance is only useful if these apply:
- you do not want to use
async/await
- your method returns a Task (effectively
void
if you were to make it not asynchronous)
- your code has logic where a task might not be returned
Example:
public static Task F()
{
if (someConditionToShortCircutTheMethod)
return Task.CompletedTask;
// rest of logic
return Task.Delay(1000);
}
As soon as you make your method async
then the fact that a Task is actually being returned is abstracted. This can make the code easier to work with especially if there are multiple points where the code waits for I/O operations to complete.
public static async Task F()
{
if (someConditionToShortCircutTheMethod)
return;
// rest of logic
await Task.Delay(1000);
await Task.Delay(1000);
// no return necessary, async Task has a similar flow as return type void
}
I also recommend reviewing these previous questions: