Do you know when to implement IDisposable?
14/01/2019 12:42 PM by
If you access unmanaged resources (e.g. files, database connections etc.) in a class, you should implement IDisposable and overwrite the Dispose method to allow you to control when the memory is freed. If not, this responsibility is left to the garbage collector to free the memory when the object containing the unmanaged resources is finalised. This means the memory will be unnecessarily consumed by resources which are no longer required, which can lead to inefficient performance and potentially running out of memory altogether.
public class MyClassFigure: Bad example - Using unmanaged resources without disposing of them when we are done
private File myFile = File.Open(...); // This is an unmanaged resource
//elsewhere in project:
private void useMyClass()
var myClass = new MyClass();
Here we are using an unmanaged resource without disposing of it, meaning it will hang around in memory unnecessarily until the garbage collector finalises it
public class MyClass : IDisposableFigure: Good example - Implementing IDisposable allows you to dispose of the unmanaged resources deterministically to maximise efficiency
private File myFile = new File.Open(...); // This is an unmanaged resource
public void Dispose()
myFile.Dispose(); // Here we dispose of the unmanaged resource
GC.SuppressFinalize(this); // Preventing a redundant garbage collector finalize call
Now we can use the "using" statement to automatically dispose the class when you are finished with it.:
private void useClass()Figure: Good example - With the using statement, the unmanaged resources are disposed of as soon as we are finished with them
using (var myClass = new MyClass())
// do stuff with myClass here...
} // myClass.Dispose() is automatically run at the end of the using block
See here for more details.
Do you feel this rule needs an update?