More than two years ago I wrote about how you should never write asynchronous methods that return
void. Writing asynchronous code became the norm and we should revisit key points about writing asynchronous functions.
Here are a couple of guidelines when writing asynchronous code in C#:
- Always add suffix Async to the end of the method. Readability beats terseness in this case.
- Always return either
- If you really have to write
async voidfunction e.g. when implementing an interface or adding an event handler, consider wrapping the code in
try-catchand handle exceptions that way.
Keep in mind that handling exceptions even when method returns
Task is something that you have to keep in mind.