Tutorial by Examples

Managed resources are resources that the runtime's garbage collector is aware and under control of. There are many classes available in the BCL, for example, such as a SqlConnection that is a wrapper class for an unmanaged resource. These classes already implement the IDisposable interface -- it's u...
It's important to let finalization ignore managed resources. The finalizer runs on another thread -- it's possible that the managed objects don't exist anymore by the time the finalizer runs. Implementing a protected Dispose(bool) method is a common practice to ensure managed resources do not have t...
.NET Framework defines a interface for types requiring a tear-down method: public interface IDisposable { void Dispose(); } Dispose() is primarily used for cleaning up resources, like unmanaged references. However, it can also be useful to force the disposing of other resources even though ...
It's fairly common that you may create a class that implements IDisposable, and then derive classes that also contain managed resources. It is recommendeded to mark the Dispose method with the virtual keyword so that clients have the ability to cleanup any resources they may own. public class Paren...
When an object implements the IDisposable interface, it can be created within the using syntax: using (var foo = new Foo()) { // do foo stuff } // when it reaches here foo.Dispose() will get called public class Foo : IDisposable { public void Dispose() { Console.WriteL...

Page 1 of 1