简体   繁体   中英

Using a generic type as a return type of an async method

A previous question made me wonder why the following method would raise a compile time error:

The return type of an async method must be void, Task or Task

public async T MyMethodAsync<T>() where T : Task
{
     // Irrelevant code here which returns a Task
}

Since we know at compile time that T is always a Task or a derived type, why won't this work?

Edit

The reason I'm asking is that a method may return a Task or a Task<T> . Let's say the method can return either and I don't want to duplicate code.

Of course this is all theoretical and isn't ment for production purposes.

Edit 2

Found a great article by Lucian Wischik: Why must async return Task

Three problems:

  • Just because T is " Task or a derived type" doesn't mean that it's Task or Task<T> . What would you expect if I called MyMethodAsync<MyCustomTask> where MyCustomTask derives from Task ?

  • The compiler needs to know whether it's building a state machine returning Task or Task<T> when it compiles the method - it uses different helper classes in the different cases

  • If an async method has a return type of Task , any return statements can't specify a value; if it has a return type of Task<T> any return statements must specify a value which is implicitly convertible to T . How can that work within MyMethodAsync ? It's a bit like saying "my method is either void or returns a T - you can decide when you call it".

It's not clear what you're trying to achieve here, but basically this isn't going to work.

I cannot think of a valid definition of MyMethodAsync that would allow it to return a generic T derived from Task without knowing at compile time what that type is or taking a parameter of some kind.

If you are really returning a Task or Task<T> then you can update your signature to reflect that fact and avoid the problem.

If you honestly need some type derived from Task then you will need to rewrite your logic to instead return Task or Task<T> and wrap around that other type. Assuming that is unacceptable you will need to drop the async and handle the state machine yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM