João Simões
1 min readJul 26, 2023

--

Good article, just some small notes.

Async void should be avoided but it is supported by the language primarily due to events, like when you click a button on some WPF or Windows Forms app, and you need to do heavy work without blocking the UI thread while ensuring the context is kept (for example, UI culture) when you resume your work. It does reduce the code you have to write (a simple "async void" instead of using explicitly the task factory to create a task with captured context) but I also have mixed feelings about with. I can understand why Microsoft did it (syntax sugar) but considering these scenarios are rare and should already be wrapped by some exception handling code (show some error modal), I would prefer not implementing "async void" and making the developer follow the explicit approach. Luckily you can have code analysis to prevent bugs like that in your APIs.

Other note, in the stream code, I think you forgot the "await" keyword on the snippet with "stream.FlushAsync".

--

--

João Simões
João Simões

Written by João Simões

Solutions Architect trying to solve world “problems”! Creator of https://code-corner.dev

No responses yet