Async generic delegate in C# 5.0

徘徊边缘 提交于 2019-12-11 02:26:14

问题


With Iterators, the following generic delegate is possible:

public delegate IEnumerable<TOut> MyDelegate<TIn>(TIn param1);

With the new async/await in C# 5.0 CTP, I expect to be able to create the analogous delegate as follows:

public delegate async TOut MyDelegate<TIn>(TIn param1);

I can't find the C# 5.0 spec or any help in this regard. Anyone know how this can be written or if it can't be written and why?

Thanks!


回答1:


async is an implementation detail, not an interface specification. An async delegate doesn't make sense.

Any method that returns an "awaitable" (such as Task or Task<T>) can be used with await.

So an "asynchronous delegate" would be any delegate type that returns Task or Task<T> (or any other kind of awaitable). In your case:

public delegate Task<TOut> MyDelegate<TIn, TOut>(TIn param1);


来源:https://stackoverflow.com/questions/8407227/async-generic-delegate-in-c-sharp-5-0

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