7 tips for converting C# code to async/awaitMar 16, 2020 · 2 minute read · Comments
Over the past year I’ve moved from working mainly in Java, to working mainly in C#. To be honest, Java and C# have more in common than not, but one of the major differences is async/await. It’s a really powerful tool if used correctly, but also a very quick way to shoot yourself in the foot.
Asynchronous programming looks very similar to synchronous programming. However, there are some core concepts which need to be understood in order to form a proper mental model when converting between synchronous and asynchronous programming patterns.
Here are some of the most common ones I’ve come across.
Method names must use the suffix Async when returning a
Task<T>. Consistency is key as the
Async suffix provides not only a mental signal to the caller that the await keyword should be used, but also provides a consistent naming convention.
Every async method returns a
Task when there is no specific result for the method, which is synonymous with
Task<T> when a return value is required.
There is not a way for the compiler to manage
out parameters. (That’s a topic for another time.) When multiple values need to be returned you should either use custom objects or a
Following up on the lack of the
void return type, no async method should be defined as an
Action variant. When accepting a delegate to an asynchronous method, the asynchronous pattern should be propagated by accepting
In asynchronous programming there is no concept of a
void return type, as the basis of the model is that each method returns a mechanism for signalling completion of the asynchronous work. When converting base classes which have empty implementations or return constant values, the framework provides methods and helpers to facilitate the pattern.
Like delegates, interfaces should always be declared async which ensures an async-aware model throughout the stack.
In certain cases, mostly unit test mocks, you may find the need to implement interfaces without having any reason to actually perform any asynchronous calls. In these specific cases it is OK to feign asynchronous execution using
Overall asynchronous programming is much better for performance, but requires a slightly different mental model. I hope these tips help!