Jason Bock

Home Biography Speaking Articles Books Music

Dispose Method to Null

I wish the following code never existed … but it did:

public class Source
{
  private XmlDocument document;

  // ...

  public void Dispose()
  {
    this.document = null;
  }
}

The class doesn’t implement IDisposable, but it has a Dispose() method. Go duck-typing! And … it sets an XmlDocument (which doesn’t implement IDisposable) to null. Which, of course, will immediately free memory and … oh wait, never mind.

What’s worse is that it was used by other sections in the code without using using:

var source = new Source();

// ...

source.Dispose();

I should stress the words “was used” - that code has been destroyed :).

Published: 08.19.2008 10:26:11 AM CST