Implementing the IDisposable pattern
In this section, we will implement a reusable IDisposable pattern. We will have a base class that implements IDisposable. This base class will provide two methods that subclasses can override. One method will be for cleaning up managed resources, and the other method will be for disposing of unmanaged resources. For us to implement the IDisposable pattern, proceed as follows:
- Add a new class called
DisposableBasethat implementsIDisposable, as follows:public class DisposableBase : IDisposable { Â Â Â Â Â Â public void Dispose() Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â Dispose(true); Â Â Â Â Â Â } Â Â Â Â Â Â private void Dispose(bool disposing) Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â Â if (disposing) Â Â Â Â Â Â Â Â Â Â GC...