Skip to main content

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

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some methods have to return a Task<T>, because an interface, a delegate or a library says so, yet they already have the answer. Task.FromResult wraps that answer in a task that has already finished, so the method can return it without async and await.

Syntax​

Syntax
Task<<Type>> <task> = Task.FromResult(<value>);
Task<<Type>> <task> = Task.FromResult<<Type>>(<value>);
PartWhat it does
Task.FromResult(<value>)A task that's already finished, with <value> as its result
<Type>The result's type. C# works it out from <value>, unless you write it in the angle brackets

Microsoft's Task.FromResult reference: "Creates a Task<TResult> that's completed successfully with the specified result."

The Example​

The program makes a finished task holding 3, checks that it's done, and awaits its result. Then a method that must return Task<string> builds a greeting and returns it with Task.FromResult.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which makes a finished task holding 3 with Task.FromResult, prints whether it's completed and its awaited result, then prints the awaited greeting of GetGreetingAsync for Ana, a method that returns Task of string with Task.FromResult.

main.cs
Task<int> count = Task.FromResult(3);
Console.WriteLine($"Finished already: {count.IsCompleted}");
Console.WriteLine($"Result: {await count}");

Console.WriteLine(await GetGreetingAsync("Ana"));

static Task<string> GetGreetingAsync(string name)
{
return Task.FromResult($"Hello, {name}!");
}

GetGreetingAsync isn't async: it builds the text at once, and Task.FromResult turns it into the Task<string> the method promises.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Finished already: True
Result: 3
Hello, Ana!

A zsh terminal where dotnet run main.cs prints Finished already: True, Result: 3 and Hello, Ana!

  1. IsCompleted was True before anything awaited it: the task finished when it was made.
  2. await count gave its result, 3, without waiting.
  3. The caller awaits GetGreetingAsync like any other task-returning method.

The command is the same in zsh, bash and PowerShell.

Without Task.FromResult​

Returning the string itself from the same method doesn't compile:

main.cs
static Task<string> GetGreetingAsync(string name)
{
return $"Hello, {name}!";
}
Output
/Users/you/demo/main.cs(5,12): error CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'

The build failed. Fix the build errors and run again.

A string isn't a Task<string>. Either wrap it with Task.FromResult, or mark the method async, which lets it return the string as is.

A Result That's null​

To return null as the result, name the type in angle brackets, with ? because it may be null. Task.FromResult(null) alone doesn't build: C# can't tell which type the task holds, and stops with error CS0411.

main.cs
string? found = await FindNicknameAsync("Ana");
Console.WriteLine(found is null ? "No nickname" : found);

static Task<string?> FindNicknameAsync(string name)
{
return Task.FromResult<string?>(null);
}
Output
No nickname

Common Mistakes​

MistakeWhat happensDo this instead
Returning the value itself from a non-async Task<T> methoderror CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'return Task.FromResult(value);
Task.FromResult(null), without a typeerror CS0411: The type arguments for method 'Task.FromResult<TResult>(TResult)' cannot be inferred from the usage. Try specifying the type arguments explicitly.Task.FromResult<string?>(null)

Troubleshooting​

SymptomLikely causeFix
CS0029 ... to 'System.Threading.Tasks.Task<...>'A plain value returned where a task is expectedWrap it with Task.FromResult
CS0411 ... 'Task.FromResult<TResult>(TResult)' cannot be inferrednull alone doesn't say which type the task holdsName it: Task.FromResult<string?>(null)

FAQ​

Is Task.FromResult the same as Task.CompletedTask?​

Task.CompletedTask is a finished task with no result, for methods that return Task. Task.FromResult is for Task<T>, when there's a result to hand back.

Does awaiting a finished task wait?​

No. Its result is already there, so await takes it and carries on, as Result: 3 showed.

Conclusion​

Task.FromResult(value) gives you a Task<T> that has already finished with value. Use it when a method must return a task but has its answer straight away, and name the type in angle brackets when the value is null.

Additional Resources​