What is the use for Task.FromResult<TResult> in C#

.net, async-await, c#, task, task-parallel-library

Solution

There are two common use cases I've found:

- When you're implementing an interface that allows asynchronous callers, but your implementation is synchronous.

- When you're stubbing/mocking asynchronous code for testing.

Problem

In C# and TPL (Task Parallel Library), the `Task` class represents an ongoing work that produces a value of type T. I'd like to know what is the need for the Task.FromResult method ? That is: In a scenario where you already have the produced value at hand, what is the need to wrap it back into a Task? The only thing that comes to mind is that it's used as some adapter for other methods accepting a Task instance.

Original source

Related problems