The awaitable and awaiter In C# 5.0 Asynchronous

后端 未结 3 1847
醉酒成梦
醉酒成梦 2021-02-07 18:48

Task or Task object is awaitable, so we can use await key on those whose return value is Task or Task. Task or Task are the most fr

3条回答
  •  渐次进展
    2021-02-07 19:20

    It is best answered in Lucian Wischik's blog post Why must async methods return Task?

    In summary (and I am not doing the blog post justice, you should read it), the issue is that Task already exists, so introducing an interface would mean

    • All the internal methods would need to be changed to the interface, a break change and thus almost impossible for the framework people to willingly do.
    • As a programmer you would constantly need to decide if you want to return Task or the interface, a decision that doesn't matter much.
    • The compiler would always need a concrete type, so even if you returned an interface from a method then it would still be compiled as Task.

    The impact from the above is so massive that it doesn't make sense to provide an interface.

提交回复
热议问题