await
doesn't matter in this case. Each async
method will generate a state machine (even if it has no await
s at all).
You can see that with this TryRoslyn example.
If you have cases where a state machine isn't needed, where the method doesn't really need to be async
like this one for example:
private async Task<string> D()
{
var data = await FetchFromDB();
return data;
}
You can remove the async
keyword and the state machine that comes with it:
private Task<string> D()
{
return FetchFromDB();
}
But otherwise, you actually need the state machine and an async
method can't operation without it.
You should note that the overhead is quite small and is usually negligible compared to the resources saved by using async-await
. If you realize that's not the case (by testing) you should probably just make that operation synchronous.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…