Why can't the return type of an async method be from Task?

I have a class (simplified sample and only the relevant parts are shown)

public class AsyncScenario : Task<Scenario>
{
    public async AsyncScenario Test(Func<Task> action)
    {
        return await this;           
    }
}

I can not compile this because the return type of the method:

Compiler Error CS1983: "The return type of the async method must be invalid, Task or Task <T>"

Why is it not allowed for the async method to return a class derived from Task<T>?

+4
source share
2 answers

# ? - , , . .

, , async return, , ? , await this Scenario... # AsyncScenario ?

, , , - . , , , , , , , , , , return.

, , , , .


† , : async ?, Lucian Wischik, VB.NET( #). . Jon Skeet answer at .

+7

, GetAwaiter :

    public static async Task<Scenario> Test()
    {
        return await new AsyncScenario();
    }

    public class AsyncScenario
    {
        public TaskAwaiter<Scenario> GetAwaiter()
        {
            return new TaskAwaiter<Scenario>();
        }
    }
+1

All Articles