Search code examples
async-ctpc#-5.0

Async generic delegate in C# 5.0


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!


Solution

  • 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);