What's the difference between returning void and returning a Task?
问题:
In looking at various C# Async CTP samples I see some async functions that return void
, and others that return the non-generic Task
.
I can see why returning a Task<MyType>
is useful to return data to the caller when the async operation completes, but the functions that I've seen that have a return type of Task
never return any data.
Why not return void
?
如果返回值是Task的时候,在await的时候,Resharper提示:Awaited Task returns no value.
回答:
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.)
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2016-03-01 Unicode and .NET
2016-03-01 tracert 路由跟踪程序