In my understanding, calling Dispose()
is necessary only when it's locking resources you need later (like a particular connection). It's always recommended to free resources you're no longer using, even if you don't need them again, simply because you shouldn't generally be holding onto resources you're not using (pun intended).
The Microsoft example is not incorrect, necessarily. All resources used will be released when the application exits. And in the case of that example, that happens almost immediately after the HttpClient
is done being used. In like cases, explicitly calling Dispose()
is somewhat superfluous.
But, in general, when a class implements IDisposable
, the understanding is that you should Dispose()
of its instances as soon as you're fully ready and able. I'd posit this is particularly true in cases like HttpClient
wherein it's not explicitly documented as to whether resources or connections are being held onto/open. In the case wherein the connection will be reused again [soon], you'll want to forgo Dipose()
ing of it -- you're not "fully ready" in that case.
See also: IDisposable.Dispose Method and When to call Dispose