What's the difference between returning void and returning a Task?
Solution 1:
SLaks and Killercam's answers are good; I thought I'd just add a bit more context.
Your first question is essentially about what methods can be marked async
.
A method marked as
async
can returnvoid
,Task
orTask<T>
. What are the differences between them?
A Task<T>
returning async method can be awaited, and when the task completes it will proffer up a T.
A Task
returning async method can be awaited, and when the task completes, the continuation of the task is scheduled to run.
A void
returning async method cannot be awaited; it is a "fire and forget" method. It does work asynchronously, and you have no way of telling when it is done. This is more than a little bit weird; as SLaks says, normally you would only do that when making an asynchronous event handler. The event fires, the handler executes; no one is going to "await" the task returned by the event handler because event handlers do not return tasks, and even if they did, what code would use the Task for something? It's usually not user code that transfers control to the handler in the first place.
Your second question, in a comment, is essentially about what can be await
ed:
What kinds of methods can be
await
ed? Can a void-returning method beawait
ed?
No, a void-returning method cannot be awaited. The compiler translates await M()
into a call to M().GetAwaiter()
, where GetAwaiter
might be an instance method or an extension method. The value awaited has to be one for which you can get an awaiter; clearly a void-returning method does not produce a value from which you can get an awaiter.
Task
-returning methods can produce awaitable values. We anticipate that third parties will want to create their own implementations of Task
-like objects that can be awaited, and you will be able to await them. However, you will not be allowed to declare async
methods that return anything but void
, Task
or Task<T>
.
(UPDATE: My last sentence there may be falsified by a future version of C#; there is a proposal to allow return types other than task types for async methods.)
(UPDATE: The feature mentioned above made it in to C# 7.)
Solution 2:
In case the caller wants to wait on the task or add a continuation.
In fact, the only reason to return void
is if you cannot return Task
because you're writing an event handler.
Solution 3:
Methods returning Task
and Task<T>
are composable - meaning that you can await
them inside of an async
method.
async
methods returning void
are not composable, but they do have two other important properties:
- They can be used as event handlers.
- They represent a "top-level" asynchronous operation.
The second point is important when you're dealing with a context that maintains a count of outstanding asynchronous operations.
The ASP.NET context is one such context; if you use async Task
methods without awaiting them from an async void
method, then the ASP.NET request will be completed too early.
Another context is the AsyncContext
I wrote for unit testing (available here) - the AsyncContext.Run
method tracks the outstanding operation count and returns when it's zero.