You cannot return the task if the method itself is declared as async - so this will not work, for example:
async Task BarAsync() { return BazAsync();
This will require the return type Task<Task> .
If your method just does a little work and then calls only one asynchronization method, then your first option is great and means that it involves even fewer tasks. You should be aware that any exceptions that occur in your synchronous method will be passed synchronously, although, indeed, that is how I prefer to handle argument checking.
It is also a general template for implementing overload, for example. using a cancel token.
Just keep in mind that if you need to switch to expecting something else, you need to make the async method instead. For example:
// Version 1: Task BarAsync() { // No need to gronkle yet... return BazAsync(); } // Oops, for version 2 I need to do some more work... async Task BarAsync() { int gronkle = await GronkleAsync(); // Do something with gronkle // Now we have to await BazAsync as we're now in an async method await BazAsync(); }
Jon Skeet Jul 26 '13 at 17:03 2013-07-26 17:03
source share