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
DisposableBase
that implementsIDisposable
, as follows:public class DisposableBase : IDisposable { public void Dispose() { Dispose(true); } private void Dispose(bool disposing) { if (disposing) GC...