The awaitable and awaiter In C# 5.0 Asynchronous

跟風遠走 提交于 2019-12-03 11:27:08

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.

This is in line with what they did for the foreach keyword (see section 8.8.4 of the C# language specification "The foreach statement").

Basically, it's duck-typing; if the type implements a MoveNext method and a Current property, that's all that's needed for the C# compiler to know how to iterate through a sequence exposed by an object.

This also applies with collection initializers (see section 7.6.10.3 of the C# language specification "Collection Initializers"); the only requirement is that the type implements the System.Collections.IEnumerable interface and have an Add method.

That said, the await keyword just sticks to prior precedent, not requiring specific interface implementations (although the interfaces supply those methods if you choose to use them), just a pattern of methods that the compiler can recognize.

I think the main reason is what you stated in point #1

instance method or extension method

Simply, because they want to enable the user to make an object Awaitable by defining an extension method for it, hence, you can make an object Awaitable even if you don't own it.

Checkout this article

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!