Lola Wecv
Lola Wecv

Reputation: 109

Odd definition of method with "async" and return type nongeneric "Task"?

A method defined as below cannot return anything. So how does it fit into the normal syntax that such a method has to return "Task". For this reason, the method below will not compile when "async" is removed.

static async Task MyAsyncMethod() { }

Also when you call the method with:

await MyAsyncMethod();

"await" is supposed to be called on awaitable object, but here the return type is "struct Void" which is not awaitable

Upvotes: 0

Views: 96

Answers (1)

Sir Rufo
Sir Rufo

Reputation: 19106

You can

public async Task DoAsync()
{
    // do anything synchron here
    // await other Task
}

or

public Task DoAsync()
{
    // do anything synchron here
    return Task.CompletedTask;
}

or

public Task DoAsync()
{
    // do anything synchron here
    return Task.FromResult( false );
}

Upvotes: 2

Related Questions