This is a mirror of official site: http://jasper-net.blogspot.com/

How to Implement IDisposable and Finalizers: 3 Easy Rules

| Monday, February 11, 2013
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

QR: Inline image 1

Posted via email from Jasper-net

0 comments: