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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…