Microsoft's documentation on IDisposable is needlessly confusing. It really boils down to three simple rules.
Rule 1: Don't do it (unless you need to).
There are only two situations when IDisposable does need to be implemented:
The class owns unmanaged resources.
The class owns managed (IDisposable) resources.
See The First Rule of Implementing IDisposable and Finalizers for more details.
Rule 2: For a class owning managed resources, implement IDisposable (but not a finalizer)
This implementation of IDisposable should only call Dispose for each owned resource. It should not have any other code: no "if" statements, no setting anything to null; just calls to Dispose or Close.
The class should not have a finalizer.
See The Second Rule of Implementing IDisposable and Finalizers for more details.
Rule 3: For a class owning a single unmanaged resource, implement both IDisposable and a finalizer
Read more: Stephen Cleary (the blog)
QR:
0 comments:
Post a Comment