I have the following methods:
private async Task<T> GetStuff<T>() {}
private async Task<T> DoStuff<T>(int id, Func<Task<T>> func) {}
My question here is this: What is the appropriate way use the lambda expression for GetStuff()
when invoking the DoStuff()
method, with regards to async
and await
?
public async Task<T> InvokeStuff<T>()
{
// option 1: keeping it simple
return await DoStuff(1, () => GetStuff<T>());
// option 2: verbose
return await DoStuff(1, async () => await GetStuff<T>());
// option 3: ???
}
The difference here is wether or not one sets the async
keyword at the lambda input parameters and an await
at the expression body's invocation of other async methods.
As far as I can tell, both option 1
and option 2
seem to work fine, which makes me think option 2 is simply redundant or possibly harmful.
-S
Edit: It has been suggested that this is a duplicate of What is the purpose of "return await" in C#?
However, that is a gross simplification. The other issue deals with the simple use of an await
within an async
method context. This is a question on wether or not to introduce the async
context when declaring a lambda expression.