Samy Sammour
Samy Sammour

Reputation: 2465

Calling await on the task directly. C#

I am learning await/async right now. and I have a question: Is there a difference between calling the await directly on the task and call it later?

var task = getAllAsync();
...
var list = await task;

OR

var list = await getAllAsync();

If there is a difference, what is it?

Upvotes: 1

Views: 116

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

If you await immediately after assigning "task" to a variable there is no difference. If you have code between method call and await-ing you have chance to execute operations in parallel.

Most tasks are created "hot" - they already have started operation (like reading file). As result if you have some code before await that code may execute while operation started by task is going on separately. I.e. you can start multiple tasks and than wait for all of them to complete - Running multiple async tasks and waiting for them all to complete.

Upvotes: 4

Related Questions