Skip to main content

How to Return a Finished Task with Task.CompletedTask in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

return Task.CompletedTask; lets a method that returns a Task finish without waiting for anything. Use it when an interface or a base class asks for a Task, but your version of the method does its work straight away.

Microsoft's reference for Task.CompletedTask: Gets a task that has already completed successfully. A Task stands for work that may finish later, and callers await it. Sometimes a method has to return a Task, because an interface declares it that way, but its own work is done at once. It can't return nothing, so it returns a task that's already finished. The reference's remarks: This property returns a task whose Status property is set to RanToCompletion.

Syntax​

Syntax
public Task <MethodName>(<parameters>)
{
<statements>
return Task.CompletedTask;
}
PartWhat it is
public Task <MethodName>(<parameters>)A method that returns a Task, often because an interface or a base class declares it. No async keyword
<statements>The method's work, done straight away
return Task.CompletedTask;Hands back a task that has already finished successfully

The Example​

A console app called TaskCompletedTask has an interface, IReminder, with one method: Task SendAsync(string message). ConsoleReminder prints the message straight away and returns Task.CompletedTask. DelayedReminder waits one second first, so it's an async method with an await. The program uses both the same way, and checks whether each task had already finished when SendAsync returned it.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n TaskCompletedTask
cd TaskCompletedTask
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/TaskCompletedTask/TaskCompletedTask.csproj:
Determining projects to restore...
Restored /Users/you/source/TaskCompletedTask/TaskCompletedTask.csproj (in 43 ms).
Restore succeeded.

dotnet new console -n TaskCompletedTask creates a folder named TaskCompletedTask with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or TaskCompletedTask.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the code that uses the two reminders:

Program.cs (use two reminders)
// Two reminders that callers use the same way: await reminder.SendAsync(...).
IReminder quick = new ConsoleReminder();
IReminder slow = new DelayedReminder();

Task first = quick.SendAsync("Water the plants");
Console.WriteLine($"ConsoleReminder finished at once: {first.IsCompleted}");
await first;

Task second = slow.SendAsync("Feed the cat");
Console.WriteLine($"DelayedReminder finished at once: {second.IsCompleted}");
await second;

Both reminders are used the same way: call SendAsync, then await the task it returns. Before each await, IsCompleted says whether that task had already finished.

Then the interface:

Program.cs (the interface)
// The interface returns a Task, so a reminder that has to wait can fit it too.
interface IReminder
{
Task SendAsync(string message);
}

SendAsync returns a Task, so every reminder has to return one, whether it waits for something or not.

Then the reminder that has nothing to wait for:

Program.cs (return Task.CompletedTask)
// Nothing to wait for: it prints, then returns a task that has already finished.
class ConsoleReminder : IReminder
{
public Task SendAsync(string message)
{
Console.WriteLine($"Now: {message}");
return Task.CompletedTask;
}
}

ConsoleReminder.SendAsync has no async keyword and no await. It prints the message, then return Task.CompletedTask; hands back a task that has already finished.

Last, a reminder that really waits:

Program.cs (a reminder that waits)
// Really waits, so it's an async method with an await.
class DelayedReminder : IReminder
{
public async Task SendAsync(string message)
{
await Task.Delay(TimeSpan.FromSeconds(1));
Console.WriteLine($"After 1 second: {message}");
}
}

DelayedReminder.SendAsync is an async method: await Task.Delay(...) waits one second, and its task finishes after that.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Now: Water the plants
ConsoleReminder finished at once: True
DelayedReminder finished at once: False
After 1 second: Feed the cat

macOS-style terminal window titled Terminal — TaskCompletedTask showing dotnet run printing Now: Water the plants, ConsoleReminder finished at once: True, DelayedReminder finished at once: False, and After 1 second: Feed the cat

ConsoleReminder printed Now: Water the plants before its task came back, and that task had already finished: True. DelayedReminder's task hadn't: False, and its message came After 1 second. The calling code didn't need to know the difference. It awaited both tasks the same way.

The Complete File​

macOS-style code window titled Program.cs — TaskCompletedTask showing two reminders used through the IReminder interface and awaited, a ConsoleReminder whose SendAsync prints the message and returns Task.CompletedTask, and a DelayedReminder whose async SendAsync awaits Task.Delay for one second

Program.cs
// Two reminders that callers use the same way: await reminder.SendAsync(...).
IReminder quick = new ConsoleReminder();
IReminder slow = new DelayedReminder();

Task first = quick.SendAsync("Water the plants");
Console.WriteLine($"ConsoleReminder finished at once: {first.IsCompleted}");
await first;

Task second = slow.SendAsync("Feed the cat");
Console.WriteLine($"DelayedReminder finished at once: {second.IsCompleted}");
await second;

// The interface returns a Task, so a reminder that has to wait can fit it too.
interface IReminder
{
Task SendAsync(string message);
}

// Nothing to wait for: it prints, then returns a task that has already finished.
class ConsoleReminder : IReminder
{
public Task SendAsync(string message)
{
Console.WriteLine($"Now: {message}");
return Task.CompletedTask;
}
}

// Really waits, so it's an async method with an await.
class DelayedReminder : IReminder
{
public async Task SendAsync(string message)
{
await Task.Delay(TimeSpan.FromSeconds(1));
Console.WriteLine($"After 1 second: {message}");
}
}

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the returnWithout return Task.CompletedTask;, the method didn't build: error CS0161: 'ConsoleReminder.SendAsync(string)': not all code paths return a valueEnd the method with return Task.CompletedTask;
Writing return; on its ownreturn; didn't build: error CS0126: An object of a type convertible to 'Task' is required. Without async, the method has to return a task objectWrite return Task.CompletedTask;
Returning nullreturn null; built with warning CS8603: Possible null reference return., and then await stopped the program: Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.Never return null from a method that returns a Task. Return Task.CompletedTask, which the example awaited without a problem

FAQ​

What if the method has to return a value?​

Then it returns a Task<T>, and Task.CompletedTask doesn't fit. The reference points to the method to use instead: To create a task that returns a value and runs to completion, call the FromResult method.

Is Task.CompletedTask the same task every time?​

It was in this run, but don't count on it:

Program.cs (the same task?)
Task one = Task.CompletedTask;
Task two = Task.CompletedTask;
Console.WriteLine($"Status: {one.Status}");
Console.WriteLine($"Same object both times: {ReferenceEquals(one, two)}");
Output
Status: RanToCompletion
Same object both times: True

The reference: Repeated attempts to retrieve this property value may not always return the same instance. So don't compare a task with Task.CompletedTask to find out whether it has finished. Read its IsCompleted or Status, as the example and this program do.

Conclusion​

Task.CompletedTask is a task that has already finished successfully. Return it from a method that has to return a Task but has nothing to wait for, without making the method async. Never return null instead, and use Task.FromResult when the task has to carry a value.

Additional Resources​