Do using statements and await keywords play nicely in c#

Yes, that should be fine.

In the first case, you’re really saying:

  • Asynchronously wait until we can get the response
  • Use it and dispose of it immediately

In the second case, you’re saying:

  • Asynchronously wait until we can get the response
  • Asynchronously wait until we’ve logged the response
  • Dispose of the response

A using statement in an async method is “odd” in that the Dispose call may execute in a different thread to the one which acquired the resource (depending on synchronization context etc) but it will still happen… assuming the thing you’re waiting for ever shows up or fail, of course. (Just like you won’t end up calling Dispose in non-async code if your using statement contains a call to a method which never returns.)

Leave a Comment