It’s very usual to develop code that will work with files, database connections and other kind of objects that consume unmanaged resources.
If you are consuming third part objects or .NET native ones, you may check if the object implements the IDisposable interface. A quick look at the list of available methods will give you the answer. Even if you have not heard about the interface yet, you have probably seen the Dispose method somewhere.
It’s a convention that the Dispose method releases managed and unmanaged resources and the public Dispose method provides a way to explicitly release resources from your calling code.
So, today’s tip is very straightforward, easy to implement and it is matter of discipline to apply it.
The code snippet uses a DataSet named ds. The first step is to check if the object has not been disposed yet and then calls the Dispose method to explicitly release resources and finally let the Garbage Collector knows that the object is not necessary anymore.
if (ds != null)
ds = null;
Keep your application healthy! If you use resources, release them.