Unmanaged resources
Some commonly used objects are based on unmanaged resources, such as file handles and database connections. These file handles and connections are scarce resources. When these objects lose all references, the garbage collector will eventually remove them and in the process, release the scarce resources they have in use. However, instead of waiting for the garbage collector, you'll want to release the scarce resources the moment they are no longer in use so that another thread can use them.
IDisposable
To allow your code to do this, these objects implement the interface IDisposable. This exposes the method Dispose(), which disposes the object. They also often implement a method Close() with the same functionality as Dispose() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access