You can take a look at the decompiled version of your code here
using System;
public class C {
public async System.Threading.Tasks.Task<bool> M() {
return false;
}
}
The method is compiled into a normal async one, with the state machine, it does return a Task<bool>
after all.
public class C
{
[CompilerGenerated]
private sealed class <M>d__0 : IAsyncStateMachine
{
public int <>1__state;
public AsyncTaskMethodBuilder<bool> <>t__builder;
public C <>4__this;
//called when you awaiting the Task
void IAsyncStateMachine.MoveNext()
{
int num = this.<>1__state;
bool result;
try
{
result = false; //the result is set
}
catch (Exception arg_0C_0)
{
Exception exception = arg_0C_0;
this.<>1__state = -2;
this.<>t__builder.SetException(exception);
return;
}
this.<>1__state = -2;
this.<>t__builder.SetResult(result);
}
[DebuggerHidden]
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
}
}
[DebuggerStepThrough, AsyncStateMachine(typeof(C.<M>d__0))]
public Task<bool> M()
{
// the state machine instance created
C.<M>d__0 <M>d__ = new C.<M>d__0();
<M>d__.<>4__this = this;
<M>d__.<>t__builder = AsyncTaskMethodBuilder<bool>.Create();
<M>d__.<>1__state = -1;
AsyncTaskMethodBuilder<bool> <>t__builder = <M>d__.<>t__builder;
<>t__builder.Start<C.<M>d__0>(ref <M>d__);
return <M>d__.<>t__builder.Task;
}
}
BTW, the compiler gives you a warning, because you never await
in your method:
warning CS1998: This async method lacks 'await' operators and will run
synchronously. Consider using the 'await' operator to await
non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work
on a background thread.
And you're absolutely right, you may just return a completed task instead:
using System;
using System.Threading.Tasks;
public class C {
public Task<bool> M() {
return Task.FromResult(false); //no need to await here at all
}
}
The other ways to code the method above could be found here.
using System;
using System.Threading.Tasks;
public class C {
public async Task<bool> M() {
return false;
}
public async Task<bool> M2(){
return await Task.FromResult(false);
}
public Task<bool> M3(){
return Task.FromResult(false);
}
}
I hope this will clarify things for you.