Can/should Task<TResult> be wrapped in a C# 5.0 awaitable which is covariant in TResult?

孤街醉人 提交于 2019-12-04 01:41:52

Task<T> can't be covariant in T, because it's a class. Only interfaces and delegates can have generic variance.

As for whether it's worth doing the wrapping... I guess that depends on how much you use the covariance within your project. I suspect you'll find all the wrapping and unwrapping confusing over time, to be honest - if it's not too bad to just take the hit of removing the covariance, I'd do that.

I believe that not including compiler support for the async keyword on an ITask interface was a major oversight on Microsoft's part. Luckily it's not too hard to work around this limitation.

I have implemented a covariant awaitable ITask<out TResult> interface. Its usage is pretty straightforward.

More information can be found at:

https://github.com/jam40jeff/ITask

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