Asynchronous Programming

async and await

  • async void should only be used for event handlers otherwise you should always avoid it. Instead async Task can be used which also does not require any return type.

    async void is the only way to allow asynchronous event handlers to work because events do not have return types (thus cannot make use of Task and Task<T>). Any other use of async void does not follow the TAP model and can be challenging to use, such as:

    • Exceptions thrown in an async void method can't be caught outside of that method.

    • async void methods are difficult to test.

    • async void methods can cause bad side effects if the caller isn't expecting them to be async.

Last updated