.NET async / await: in this case should I return something?

In that case, when my task could instantly return due to certain conditions:

public async Task Test(List<int> TestList) { if (TestList.Count() == 0) return; // Do something await UploadListAsync(TestList); } 

Is this correct return; whether to use Task.FromResult(false); , or is there a more correct way?

+8
c # async-await
source share
2 answers

When you use async , you cannot explicitly return Task (see edit) - the return value was inserted by the compiler as the return value of Task . Thus, your async methods should behave like other methods and return if they need to break out of the logic first.

EDIT: The only time you can return Task during async is the return type of Task<Task... , however, it will still return an internal Task , not an external one, since the compiler did the same packing. This should also be a fairly rare use case, not await - a thread from Task s

+11
source share

Task is the new void !

Regarding async methods!

0
source share

All Articles