#941 – Checking to See If Objects Are Disposable
September 30, 2013 3 Comments
If a type implements the IDisposable interface, you should always call the Dispose method on an instance of the class when you are done using it. The presence of IDisposable indicates that the class has some resources that can be released prior to garbage collection.
Below is one pattern for checking to see if an object implements the IDisposable interface.
Dog bob = new Dog("Bob", 5); bob.Bark(); if (bob is IDisposable) ((IDisposable)bob).Dispose();
You can also use the using statement to automatically call Dispose on an object when you’re done using it.