Search code examples
.netdisposeidisposableboilerplate

Is there any way to reduce the amount of boilerplate code for IDisposable?


My project has many reader and writer classes. I started implementing IDisposable, but I believe that it adds many boilerplate code to my classes. For each of the classes, I need to implement:

  • A destructor.
  • A Dispose() method.
  • A Dispose(bool disposing) method.
  • A "bool disposed" field.
  • A check to see if the object is already disposed on every single public method.
  • Documentation (since I have the documentation flag enabled and the compiler warns otherwise).

My questions:

Is it worth to implement IDisposable? Previously, I used my classes like:

using (Stream s = File.OpenRead("myfile.bin"))
    x = new SomethingReader(s).ReadSomething();

With IDisposable, it doesn't improve much:

using (SomethingReader r = new SomethingReader(File.OpenRead("myfile.bin")))
    x = r.ReadSomething();

Can I ONLY implement the Dispose() method?

My class doesn't use unmanaged resources, so Dispose(false) does nothing. Since what I'm cleaning is IDisposable too, suposedly calling my methods after the object has been disposed should throw a ObjectDisposedException since I'm using an already disposed object.

If you recommend implementing the full IDisposable pattern, is there any way to reduce the boilerplate code?

EDIT: Since some answers recommend sealing the class, I can confirm that, in fact, there isn't any problem by sealing the class.


Solution

  • If you're not holding unmanaged resources directly (i.e. even SafeHandle is not holding them directly), then in general:

    • Don't implement the finalizer
    • Don't implement Dispose(bool)
    • DO mark your class as sealed.

    It is a great pity that for years all the widely distributed documentation for IDispose was intended for people writing interop libraries (i.e. the .NET framework authors themselves), and completely ignored a much more common usage pattern for everyone else.

    Things have improved, and there is now a more lightweight pattern recognised by fxcop (VS Code Analysis) for sealed types: http://msdn.microsoft.com/en-us/library/ms244737%28VS.80%29.aspx