Reputation: 111
this is a stupid question but somehow it makes me feel I am missing something. Is there any difference in execution for async lambda and normal method? Like this
var tasks = list.Select(async c => { /* await somewhere */});
await Task.WhenAll(tasks);
and that
async Task<object> GetSomething(object c) { /* await somewhere */}
// ...
var task = list.Select(GetSomething);
await Task.WhenAll(tasks);
edit: I am asking because I have misconceptions if it is possible for the lambda to behave differently then a normal method. Provided that both the lambda and the method have the same body, is it possible that the lambda would create a void task? or the execution would not work as expected?
thank you, I haven't expected that fast response!
Upvotes: 2
Views: 89
Reputation:
A lambda creates either an anonymous method or an expression tree, depending on whether it's used in a context accepting a delegate or an Expression<...>
type. In those cases where it creates an anonymous method, it's just like what it would've been had you written the method explicitly. Captured variables may change where the method gets defined, but it'll always be a real method that's seen as such by the runtime. The async
keyword does not change that.
Upvotes: 5